2017-12-06 1 views
-1

Ich versuche, die Markierung auf meinem aktuellen Standort zu setzen, so dass ich versuchte, einen Ort zu einem LatLng Klasse zu konvertieren:Konvertieren eine Lage zu LatLng

LatLng mCurrentPlace= new LatLng(location.getLatitude(),location.getLongitude()); 

Dann erinnerte ich mich an die addMarker Methode:

mMap.addMarker(new MarkerOptions() 
    .title(getString(R.string.default_info_title)) 
    .position(mCurrentPlace) 
    .snippet(getString(R.string.default_info_snippet))) 

Aber Starten der Anwendung, die von „Run“, verhafteten sie.

Wo bin ich falsch?

Danke.

+1

verhaftet? meinst du es ist abgestürzt? Wenn ja, was ist das Traceback auf dem Logcat? – pskink

+0

verhaftet? Was meinst du mit –

+0

Wenn meine Antwort für Sie funktioniert, dann akzeptieren Sie es bitte. –

Antwort

1
public void moveMap(GoogleMap gMap, double latitude, double longitude) { 
     Log.v(TAG, "mapMoved: " + gMap); 
     LatLng latlng = new LatLng(latitude, longitude); 
     CameraUpdate cu = CameraUpdateFactory.newLatLngZoom(latlng, 6); 
     gMap.addMarker(new MarkerOptions().position(latlng)); 
     gMap.moveCamera(cu); 
    } 

Rufen Sie diese Methode, wenn Sie Standort und Marker und auf mapasync Callback-Methode wollen.

@Override 
     public void onMapReady(GoogleMap googleMap) { 

      MapsInitializer.initialize(context); 
      gMap = googleMap; 
      gMap.getUiSettings().setMapToolbarEnabled(false); 
      gMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
      if(items.get(getLayoutPosition())!=null) 
       moveMap(gMap,latitude, longitude); 
     } 
     public void initializeMapView() { 
      if (mapView != null) { 
       // Initialise the MapView 
       mapView.onCreate(null); 
       // Set the map ready callback to receive the GoogleMap object 
       mapView.getMapAsync(this); 

      } 
     } 

Aufschalten onMapReadyCallback Methode und tun dies.

Anruf initializeMapView Methode in onCreate() oder In-Adapter onBindViewHolder

0

MAP Aktivität

public class MapsActivity2 extends FragmentActivity implements 
OnMapReadyCallback, GoogleMap.OnMyLocationButtonClickListener { 

private GoogleMap mMap; 

LatLng loc; 
Location location; 
private double currentLatitude = 0; 
private double currentLongitude = 0; 
LocationManager locationManager; 


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

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

} 


/** 
* 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 atLnmove 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; 


     // 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.newLg(sydney)); 

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     // TODO: Consider calling 
     // ActivityCompat#requestPermissions 
     // here to request the missing permissions, and then overriding 
     // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
     //           int[] grantResults) 
     // to handle the case where the user grants the permission. See the documentation 
     // for ActivityCompat#requestPermissions for more details. 
     return; 
    } 
    mMap.setMyLocationEnabled(true); 
    mMap.setOnMyLocationButtonClickListener(MapsActivity2.this); 

} 



public double getLatitude(){ 
    if(location != null){ 
     currentLatitude = location.getLatitude(); 
    } 
    // return latitude 
    return currentLatitude; 
} 

public double getLongitude(){ 
    if(location != null){ 
     currentLongitude = location.getLongitude(); 
    } 
    // return longitude 
    return currentLongitude; 
} 

@Override 
public boolean onMyLocationButtonClick() { 
    location=mMap.getMyLocation(); 
    currentLatitude=getLatitude(); 
    currentLongitude=getLongitude(); 

    LatLng currentLocation = new LatLng(currentLatitude, currentLongitude); 
    mMap.moveCamera(CameraUpdateFactory.newLatLng(currentLocation)); 
    mMap.addMarker(new MarkerOptions().position(currentLocation).title("Marker in Current Location")); 


    return false; 
} 
} 

Ressourcen XML

<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" 
tools:context="com.atziant.parashar.gmapsapi.MapsActivity2" /> 
Verwandte Themen