2016-04-12 10 views
0

Iveは地図に逆ジオコーディングを追加し、地図をクリック可能に設定しました。地図をクリックすると、アドレスがマーカーの上に表示されない限り、すべてが機能します。地図に表示されている住所がありません。AndroidのGoogleマップ

latLanは配列ですが、変数を使用する唯一の方法です。私は問題がこの領域の周りにあると思うが、私の手をそれに置くことはできない。

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

}

答えて

0

はあなたのコードをテストし、エラーの原因を発見しました。

// ReverseGeocodingTaskを(実行する前にaddMarker()を実装する際にタッチした位置

mMap.addMarker(markerOptions); 

上のマーカーを配置)とonPostExecute()で再びaddMarker()を呼び出します。アドレスと上書きマーカーを表示するのに十分な大きさのカスタムマーカーイメージを追加できます。

Customizeマーカー画像

あなたは、多くの場合、アイコンと呼ばれるカスタムマーカー画像とデフォルトのマーカー画像を置き換えることができます。カスタムアイコンは常にBitmapDescriptorとして設定され、BitmapDescriptorFactoryクラスの4つのメソッドのいずれかを使用して定義されます。カスタムイメージのため

コード:

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

あなたはそれがあるべきとしてそれはあなたのアプリケーションが動作するnew ReverseGeocodingTask(getBaseContext()).execute(latLng[0]);前に最初のmMap.addMarker(markerOptions);を削除することができます。使用このコードは(!アドレス= nullを& & addresses.size()> 0){ アドレスアドレス= addresses.get(0)場合は住所

を取得するための作業かもしれませ

0

。 (0)+アドレス.getAddressLine(1)+アドレス。 }

関連する問題