2016-04-12 15 views
0

Ive Geocoding haritama eklendi ve haritayı tıklanabilir duruma getirdi. Her şey, haritayı tıklattığımda adresin işaretçinin üzerinde gösterilmemesi dışında çalışacaktır.Haritadaki hiçbir adres haritada yok Google + 'yı tıklayın

latLan bir dizidir, ancak değişkeni kullanmama izin vermenin tek yolu budur. Sanırım sorun bu alanın etrafında yatıyor ama elimi üstüne koyamıyorum.

 @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); 
    } 
} 

}

cevap

0

kodunuzu test ve hata olası nedenini buldum. onPostExecute() tekrar ReverseGeocodingTask (yapmadan önce addMarker() uygulayan) ve addMarker() çağrı üzerine dokundu pozisyon

mMap.addMarker(markerOptions); 

bir işaretleyici Yerleştirme

//. Adresi ve üzerine yazma işareti görünür hale getirmek için yeterince büyük bir özel işaretleyici görüntüsü eklemeye çalışabilirsiniz.

Customize Özel bir işaretleyici görüntü ile varsayılan işaretleyici görüntüyü değiştirebilir

işaretleyici görüntü, genellikle bir simge aradı. Özel simgeler her zaman bir BitmapDescriptor olarak ayarlanır ve BitmapDescriptorFactory sınıfındaki dört yöntemden biri kullanılarak tanımlanır. özel görüntü için

Kodu:

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))); 

Sen olması gerektiği gibi o zaman uygulama çalışacaktır new ReverseGeocodingTask(getBaseContext()).execute(latLng[0]); önce ilk mMap.addMarker(markerOptions); kaldırabilirsiniz.

0

Kullanım Bu Kod Adres

almak için çalışma olabilir

eğer (adresler = null & & addresses.size()> 0) { Adres adresi = addresses.get (0); Dize addressLine = address.getAddressLine (0) + address.getAddressLine (1) + address.getAddressLine (2) + address.getAddressLine (3); }

İlgili konular