2016-04-12 21 views
0

Ive umgekehrte Geokodierung zu meiner Karte hinzugefügt und setzen Sie die Karte anklickbar. Alles funktioniert wie es sollte, außer dass die Adresse nicht über dem Marker angezeigt wird, wenn ich auf die Karte klicke.Keine Adresse auf Karte anzeigen klicken Android Google Maps

mein LatLan ist ein Array, aber das ist die einzige Möglichkeit, die Variable zu verwenden. Ich denke, das Problem liegt in diesem Bereich, aber ich kann meine Hand nicht darauf legen.

 @Override 
public void onMapReady(GoogleMap googleMap) { 
    mMap = googleMap; 
    mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID); // changes view to hybrid 
    mMap.setMyLocationEnabled(true); // shows location on map 

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); // uses GPS only to get location 

    double currentLatitude = location.getLatitude(); 
    double currentLongitude = location.getLongitude(); 

    final LatLng[] latLng = {(new LatLng(currentLatitude, currentLongitude))}; 

    mMap.animateCamera(
      CameraUpdateFactory.newLatLngZoom(latLng[0], 18)); // This will zoom camera to updated lat and long without constant updates 

    mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {// Setting a click event handler for the map 


     @Override 
     public void onMapClick(LatLng arg0) { 
      Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); // uses GPS only to get location 
      double currentLatitude = location.getLatitude(); 
      double currentLongitude = location.getLongitude(); 

      //LatLng latLng; 

      // Getting the Latitude and Longitude of the touched location 
      latLng[0] = arg0; 

      // Clears the previously touched position 
      mMap.clear(); 

      // Animating to the touched position 
      mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng[0])); 

      // Creating a marker 
      markerOptions = new MarkerOptions(); 

      // Setting the position for the marker 
      markerOptions.position(latLng[0]); 

      // Placing a marker on the touched position 
      mMap.addMarker(markerOptions); 

      // Adding Marker on the touched location with address 
      new ReverseGeocodingTask(getBaseContext()).execute(latLng[0]); 


     } 
    }); 
    //new ReverseGeocodingTask(getBaseContext()).execute(latLng); 
} 

private class ReverseGeocodingTask extends AsyncTask<LatLng, Void, String> { 
    Context mContext; 

    public ReverseGeocodingTask(Context context) { 
     super(); 
     mContext = context; 
    } 

    @Override 
    protected String doInBackground(LatLng... params) { 
     Geocoder geocoder = new Geocoder(mContext); 
     double latitude = params[0].latitude; 
     double longitude = params[0].longitude; 

     List<android.location.Address> addresses = null; 
     String addressText = ""; 

     try { 
      addresses = geocoder.getFromLocation(latitude, longitude, 1); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     if (addresses != null && addresses.size() > 0) { 
      android.location.Address address = addresses.get(0); 
      addressText = String.format("%s, %s, %s", address.getMaxAddressLineIndex() > 0 ? 
        address.getAddressLine(0) : "", address.getLocality(), address.getCountryName()); 
     } 
     return addressText; 

    } 

    @Override 
    protected void onPostExecute(String addressText) { 

    markerOptions.title(addressText); 
    mMap.addMarker(markerOptions); 
    } 
} 

}

Antwort

0

Getestet Code und fand mögliche Fehlerursache.

// eine Markierung auf der berührten Position

mMap.addMarker(markerOptions); 

Nach addMarker() Implementierung Platzierung vor der ReverseGeocodingTask() zu tun und addMarker() wieder im onPostExecute() aufrufen. Sie können versuchen, ein benutzerdefiniertes Markierungsbild hinzuzufügen, das groß genug ist, um die Adresse und den Überschreibungsmarker sichtbar zu machen.

Customize das Markierungsbild

Sie das Standard-Markierungsbild mit einem benutzerdefinierten Markierungsbild ersetzen können, oft ein Symbol genannt. Benutzerdefinierte Symbole werden immer als BitmapDescriptor festgelegt und mithilfe einer von vier Methoden in der BitmapDescriptorFactory-Klasse definiert.

-Code für benutzerdefinierte Bild:

private static final LatLng MELBOURNE = new LatLng(-37.813, 144.962); 
private Marker melbourne = mMap.addMarker(new MarkerOptions() 
.position(MELBOURNE) 
.title("Melbourne") 
.snippet("Population: 4,137,400") 
.icon(BitmapDescriptorFactory.fromResource(R.drawable.arrow))); 

Sie können die erste mMap.addMarker(markerOptions); vor new ReverseGeocodingTask(getBaseContext()).execute(latLng[0]); entfernen dann die App wie es sein sollte funktionieren.

0

Verwendung Dieser Code kann Arbeit Adresse für das Erhalten

if (Adressen = null & & addresses.size()> 0!) { Adresse Adresse = addresses.get (0); Zeichenfolge addressLine = address.getAddressLine (0) + address.getAddressLine (1) + address.getAddressLine (2) + address.getAddressLine (3); }

Verwandte Themen