2013-03-29 32 views
2

我正在製作Google Maps Api V2的演示應用程序。轉換Lat Long以訪問谷歌地圖ApiV2

我添加了一個簡單的標記,並取得了可拖動

這裏是我的代碼:

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.demo_v2); 


     googleMap = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); 
     googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID); 
     googleMap.setOnMapClickListener(this); 
     googleMap.setOnMarkerDragListener(this); 
     googleMap.setOnMapLongClickListener(this); 


     googleMap.setInfoWindowAdapter(new InfoWindowAdapter() { 

      @Override 
      public View getInfoWindow(Marker arg0) { 

       return null; 
      } 

      @Override 
      public View getInfoContents(Marker marker) { 

       LayoutInflater inflater = (LayoutInflater) MapsDemo.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       View v = inflater.inflate(R.layout.custom_info, null); 

       TextView tv = (TextView)v.findViewById(R.id.textView1); 
       tv.setText(marker.getSnippet()); 

       return v; 
      } 
     }); 

     googleMap.setMyLocationEnabled(true); 
     googleMap.getUiSettings().setRotateGesturesEnabled(true); 
     googleMap.getUiSettings().setTiltGesturesEnabled(true); 

     marker = googleMap.addMarker(new MarkerOptions() 
     .position(ROMA) 
     .title("Hello") 
     .snippet("Nice Place") 
     .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher)) 
     .draggable(true)); 

@Override 
    public void onMarkerDrag(Marker marker) { 
     // TODO Auto-generated method stub 
    } 

    @Override 
    public void onMarkerDragEnd(Marker marker) { 
     LatLng field = marker.getPosition(); 
     System.out.println("LatitudenLongitude:"+field.latitude+" "+field.longitude); 

    } 

    @Override 
    public void onMarkerDragStart(Marker marker) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onMapLongClick(LatLng latlng) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onMapClick(LatLng latlng) { 
     // TODO Auto-generated method stub 

    } 

}

現在我想,當地址應該站出來在用戶點擊時在標記上。

簡單的問題:想從Lat Long網地址(名稱)在API第2

回答

4

您應該使用Geocoder可供Android API。您需要致電getFromLocation(latitude,longitude,maxResults),這將返回List<Address>

+0

嗨奧維迪烏。你能幫我解決這個問題嗎?我希望當我將標記拖到邊界時,地圖應該自動滾動到另一個方向。可能嗎 ? - 谷歌地圖api v2? –

+1

也許你應該爲此發佈另一個問題。 –

+0

好的,謝謝@Ovidiu –

13

試試這個:

public List<Address> getAddress() { 
    if (latitude != 0 && longitude != 0) { 
     try { 
      Geocoder geocoder = new Geocoder(context); 
      List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1); 
      String address = addresses.get(0).getAddressLine(0); 
      String city = addresses.get(0).getAddressLine(1); 
      String country = addresses.get(0).getAddressLine(2); 
      Log.d("TAG", "address = " + address + ", city = " + city + ", country = " + country); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } else { 
     Toast.makeText(context, "latitude and longitude are null", Toast.LENGTH_LONG).show(); 
    } 
    return addresses; 
} 
+0

對不起,我沒有理解你到底想要什麼?你能簡單地解釋一下還是舉例說。 –

+0

我希望當用戶開始拖動標記併到達地圖屏幕(可見部分)的邊界時,地圖屏幕應該自動滑動到另一側,以便能夠顯示更多。 –

+0

試試這個[link](http://android-er.blogspot.in/2013/01/google-maps-android-api-v2-example_7.html)希望這對你有所幫助。 –