2016-04-22 15 views
8

Sto lavorando su google maps e ho bisogno di cambiare la posizione di La mia posizione (pulsante posizione corrente). Il pulsante di posizione attuale è in alto a destra. Quindi, per favore aiutami a rimontare il pulsante della posizione corrente nella schermata di google maps. Grazie in anticipo.Come modificare la posizione del pulsante La mia posizione in Google Maps utilizzando Android studio

il mio codice di esempio:

final GoogleMap googleMap = mMapView.getMap(); 
googleMap.setMyLocationEnabled(true); 
+2

Non credo sia possibile. –

+0

formato di codice migliore e fissato alcuni formati grammaticali – Robert

risposta

8

è possibile utilizzare questo

View locationButton = ((View) mMapView.findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("2")); 
RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) locationButton.getLayoutParams(); 
// position on right bottom 
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0); 
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE); 
rlp.setMargins(0, 180, 180, 0); 
+0

per ulteriori dettagli, controllare qui http://stackoverflow.com/questions/31591524/android-align-left-aligns-to-the-center –

+0

qui in realtà: http: // stackoverflow. it/questions/28688470/android-google-maps-api-change-position-of-maps-toolbar/31337222 # 31337222 e Yashwanth ha sbagliato in doppio ALIGN-PARENT_TOP – djdance

2

Impostando imbottitura per GoogleMap Frammento è possibile modificare la posizione di myLocationButton ma unico problema è che sarà anche cambiare le posizioni di altri tasti come lo zoom.

SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); 
mapFragment.getMapAsync(this); 

@Override 
public void onMapReady(GoogleMap map) { 
    map.setPadding(left, top, right, bottom); 
} 
0

Si può fare una cosa. Disabilita il pulsante "La mia posizione" predefinito e fai un pulsante personalizzato nella posizione desiderata e con il clic di quel pulsante muovi la videocamera fino a latlng corrente.

0

Purtroppo, è possibile impostare solo imbottitura, in questo modo:

@Override 
public void onMapReady(GoogleMap googleMap) { 
    googleMap.setPadding(left, top, right, bottom); 
    ... 
} 

ho finito per creare il mio e incorporarlo in un layout di frame utilizzando layout_gravity per specificare dove voglio, in questo caso in basso/fine :

<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="300dp"> 

    <fragment 
     android:id="@+id/map" 
     android:name="com.google.android.gms.maps.SupportMapFragment" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     .../> 


    <ImageView 
     android:id="@+id/myLocationCustomButton" 
     android:layout_width="@dimen/custom_my_location_button_size" 
     android:layout_height="@dimen/custom_my_location_button_size" 
     android:layout_gravity="bottom|end" 
     android:background="@drawable/disc" 
     android:backgroundTint="@android:color/white" 
     android:padding="@dimen/margin_smallest" 
     android:src="@drawable/ic_my_location" 
     ../> 
</FrameLayout> 

Poi, nell'attività, ho inizializzato e ha iniziato un client API di Google che posso usare per andare a prendere posizione quando necessario per il tasto durante uno scatto, vedere il pulsante scatto ascoltatore di seguito:

@Override 
protected void onCreate(@Nullable Bundle savedInstanceState) { 
    ... 
    if (googleApiClient == null) { 
     googleApiClient = new GoogleApiClient.Builder(this) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .addApi(LocationServices.API) 
       .build(); 
    } 

    myLocationButton = rootView.findViewById(R.id.myLocationCustomButton); 
} 

@Override 
protected void onStart() { 
    googleApiClient.connect(); 
    super.onStart(); 
} 

@Override 
protected void onStop() { 
    googleApiClient.disconnect(); 
    super.onStop(); 
} 

@Override 
public void onConnected(@Nullable Bundle bundle) { 
    myLocationButton.performClick(); 
} 

@Override 
public void onConnectionSuspended(int i) { 
} 

@Override 
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 
} 

@Override 
public void onMapReady(final GoogleMap googleMap) { 
    this.googleMap = googleMap; 

    myLocationButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Location location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient); 
      CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 16); 
      MainActivity.this.googleMap.animateCamera(cameraUpdate, 250, null); 
     } 
    }); 

mio app di sub-modulo build.gradle ha anche:

ext { 
    playServicesVersion = "8.4.0" 
} 

dependencies { 
    ...  
    compile "com.google.android.gms:play-services-location:${playServicesVersion}" 
    ... 
} 

Speranza che aiuta.

12

Ho risolto questo problema nella mia mappa frammento da ri posizionando il pulsante di posizione verso l'angolo in basso a destra di vista utilizzando il codice qui sotto, ecco la mia MapsActivity.java: -

import android.support.v4.app.FragmentActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.RelativeLayout; 

import com.google.android.gms.maps.CameraUpdateFactory; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.OnMapReadyCallback; 
import com.google.android.gms.maps.SupportMapFragment; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.MarkerOptions; 

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { 

    private GoogleMap mMap; 
    View mapView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.fragment_map); 
     // Obtain the SupportMapFragment and get notified when the map is ready to be used. 
     SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
       .findFragmentById(R.id.map); 
     mapView = mapFragment.getView(); 
     mapFragment.getMapAsync(this); 
      } 

    /** 
    * Manipulates the map once available. 
    * This callback is triggered when the map is ready to be used. 
    * This is where we can add markers or lines, add listeners or move the camera. In this case, 
    * we just add a marker near Sydney, Australia. 
    * If Google Play services is not installed on the device, the user will be prompted to install 
    * it inside the SupportMapFragment. This method will only be triggered once the user has 
    * installed Google Play services and returned to the app. 
    */ 
    @Override 
    public void onMapReady(GoogleMap googleMap) { 
     mMap = googleMap; 
     mMap.setMyLocationEnabled(true); 

     // Add a marker in Sydney and move the camera 
     LatLng sydney = new LatLng(-34, 151); 
     mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")); 
     mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); 

     if (mapView != null && 
       mapView.findViewById(Integer.parseInt("1")) != null) { 
      // Get the button view 
      View locationButton = ((View) mapView.findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("2")); 
      // and next place it, on bottom right (as Google Maps app) 
      RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) 
        locationButton.getLayoutParams(); 
      // position on right bottom 
      layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0); 
      layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE); 
      layoutParams.setMargins(0, 0, 30, 30); 
     } 

    } 
} 

E qui è il layout del frammento: -

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.infogird.www.location_button_reposition.MapFragment"> 

    <fragment xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:map="http://schemas.android.com/apk/res-auto" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:id="@+id/map" 
     android:name="com.google.android.gms.maps.SupportMapFragment" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     /> 
</FrameLayout> 

Spero che questo risolva il tuo problema. Grazie.

+0

Dove si riallinea questo? layoutParams.addRule (RelativeLayout.ALIGN_PARENT_TOP, 0); layoutParams.addRule (RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE); –

3

È possibile utilizzare il codice qui sotto per tutte le condizioni, quali: 1. Se si desidera mostrare il pulsante posizione in alto a destra 2.If che volete mostrare il pulsante posizione in basso a destra 3.If che volete pulsante ubicazione mostra in basso a sinistra

1. Se si desidera visualizzare il pulsante posizione in alto a destra

View locationButton = ((View) mapView.findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("2")); 
      RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) locationButton.getLayoutParams(); 
      // position on right bottom 
      rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0); 
      rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE); 

2.If che volete pulsante posizione in basso a destra

View locationButton = ((View) mapView.findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("2")); 
      RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) locationButton.getLayoutParams(); 
      // position on right bottom 
      rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0); 
      rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);rlp.setMargins(0,0,30,30); 
mostrare

3.Se si desidera che il pulsante posizione nel mostrare in basso a sinistra

View locationButton = ((View) mapView.findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("2")); 
      RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) locationButton.getLayoutParams(); 
      // position on right bottom 


      rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE); 
      rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE); 
      rlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0); 
      rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0); 

      rlp.addRule(RelativeLayout.ALIGN_PARENT_END, 0); 
      rlp.addRule(RelativeLayout.ALIGN_END, 0); 
      rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT); 
      rlp.setMargins(30, 0, 0, 40); 

Buona fortuna

0

Il suo lavoro visualizzare la mia posizione del pulsante in basso a destra su MapView

XML

<com.google.android.gms.maps.MapView 
    android:id="@+id/mapView" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

JAVA

private MapView mMapView; 

mMapView = (MapView) findViewById(R.id.mapView); 

mMapView.getMapAsync(new OnMapReadyCallback() { 
    @Override 
    public void onMapReady(final GoogleMap mMap) { 
     if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { 
      mMap.setMyLocationEnabled(true); 
     } 

     if (mMapView != null && mMapView.findViewById(Integer.parseInt("1")) != null) { 
      View locationButton = ((View) mMapView.findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("2")); 
      RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) locationButton.getLayoutParams(); 
      layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0); 
      layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE); 
      layoutParams.setMargins(0, 0, 20, 20); 
     } 
    } 
}); 
0

So che è stato risposto e accettato ma queste risposte non ha funzionato per me. Forse ci sono stati cambiamenti in RelativeLayout.LayoutParams o anche in GoogleMaps da quando è stata fornita una risposta.

Nei miei tentativi con le risposte esistenti ho capito che potrebbero esserci più regole LayoutParam per il pulsante della bussola che stanno annullando i miei tentativi di spostare il pulsante della bussola dove lo volevo. Ho sperimentato rimuovendo diversi parametri come questo:

RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) 
       locationButton.getLayoutParams(); 

layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_TOP); 
layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_START); 
layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_END); 

E quindi aggiungere le nuove regole come hanno fatto in molte delle risposte. Ma ancora non ha funzionato come previsto. Più spesso che no, il pulsante è rimasto da qualche parte sul lato sinistro.

Ho deciso di creare solo nuovi layoutParams e sostituire quelli esistenti e ... ha funzionato perfettamente!

Originariamente ho utilizzato il tag vista per GoogleMapCompass, ma la domanda riguardava lo GoogleMapMyLocationButton. Così ho commentato la linea GoogleMapCompass e inserito la linea GoogleMapMyLocationButton.

Qui è il codice per MapFragment.java:

import android.app.Fragment; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.RelativeLayout; 

import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.OnMapReadyCallback; 
import com.google.android.gms.maps.SupportMapFragment; 


public class MapFragment extends Fragment implements OnMapReadyCallback { 
    private static final String TAG = MapFragment.class.getSimpleName(); 

    private SupportMapFragment mapFragment = null; 

    public MapFragment() { 
     // Required empty public constructor 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedState) { 
     // Inflate the layout for this fragment 
     View view = inflater.inflate(R.layout.fragment_map, container, false); 

     Log.d(TAG, "onCreateView()"); 

     mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map); 
     mapFragment.setRetainInstance(true); 

     mapFragment.getMapAsync(this); 

     return view.findViewById(R.id.map); 
    } 

    @Override 
    public void onMapReady(GoogleMap googleMap) { 
     Log.d(TAG, "onMapReady()"); 

     View mapView = mapFragment.getView(); 

     moveCompassButton(mapView); 
    } 

    /** 
    * Move the compass button to the right side, centered vertically. 
    */ 
    public void moveCompassButton(View mapView) { 
     try { 
      assert mapView != null; // skip this if the mapView has not been set yet 

      Log.d(TAG, "moveCompassButton()"); 

      // View view = mapView.findViewWithTag("GoogleMapCompass"); 
      View view = mapView.findViewWithTag("GoogleMapMyLocationButton"); 

      // move the compass button to the right side, centered 
      RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 

      layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE); 
      layoutParams.addRule(RelativeLayout.ALIGN_PARENT_END, RelativeLayout.TRUE); 
      layoutParams.addRule(RelativeLayout.CENTER_VERTICAL); 
      layoutParams.setMarginEnd(18); 

      view.setLayoutParams(layoutParams); 
     } catch (Exception ex) { 
      Log.e(TAG, "moveCompassButton() - failed: " + ex.getLocalizedMessage()); 
      ex.printStackTrace(); 
     } 
    } 
} 
Problemi correlati