2012-12-27 5 views
0

위치를 얻으려고하거나 위치 이름을 전달하여지도에 위치를 표시하려고합니다.
지도에서 현재 위치를 보여주는 하나의 샘플 프로젝트를 수행했습니다. 그러나 나는 Google지도에서 이름과 검색 장소를 전달하는 방법을 이해하지 못합니다.
내 문제를 이해하는 데 도움이 될 수있는 코드는 다음과 같습니다.이름으로 Google지도의 위치가 표시되지 않습니다.

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    mapView = (MapView) findViewById(R.id.mapview); 
    mapView.setBuiltInZoomControls(true); 
    mapView.setSatellite(true); 
    mapController = mapView.getController(); 
    mapController.setZoom(14); // Zoom 1 is world view 
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 
     0, new GeoUpdateHandler()); 

    myLocationOverlay = new MyLocationOverlay(this, mapView); 
    mapView.getOverlays().add(myLocationOverlay); 

    String value = "kasarwadi,pune"; 
    // Do something with value! 


    Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());  
     try { 
      List<Address> addresses = geoCoder.getFromLocationName(
       value, 5); 
      String add = ""; 
      if (addresses.size() > 0) { 
      GeoPoint p = new GeoPoint(
         (int) (addresses.get(0).getLatitude() * 1E6), 
         (int) (addresses.get(0).getLongitude() * 1E6)); 
       mapController.animateTo(p);  
       mapView.invalidate(); 
      }  
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

    myLocationOverlay.runOnFirstFix(new Runnable() { 
     public void run() { 
     mapView.getController().animateTo(myLocationOverlay.getMyLocation()); 
     } 
    }); 

    Drawable drawable = this.getResources().getDrawable(R.drawable.point); 
    itemizedoverlay = new MyOverlays(this, drawable); 
    createMarker(); 
    } 

    @Override 
    protected boolean isRouteDisplayed() { 
    return false; 
    } 

    public class GeoUpdateHandler implements LocationListener { 

    @Override 
    public void onLocationChanged(Location location) { 
     String value = "kasarwadi,pune"; 
     // Do something with value! 


     Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());  
      try { 
       List<Address> addresses = geoCoder.getFromLocationName(
        value, 5); 
       String add = ""; 
       if (addresses.size() > 0) { 
       GeoPoint p = new GeoPoint(
          (int) (addresses.get(0).getLatitude() * 1E6), 
          (int) (addresses.get(0).getLongitude() * 1E6)); 
        mapController.animateTo(p);  
        mapView.invalidate(); 
       }  
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 


    } 

    @Override 
    public void onProviderDisabled(String provider) { 
    } 

    @Override 
    public void onProviderEnabled(String provider) { 
    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
    } 
    } 

    private void createMarker() { 
    GeoPoint p = mapView.getMapCenter(); 
    OverlayItem overlayitem = new OverlayItem(p, "", ""); 
    itemizedoverlay.addOverlay(overlayitem); 
    if (itemizedoverlay.size() > 0) { 
     mapView.getOverlays().add(itemizedoverlay); 
    } 
    } 

    @Override 
    protected void onResume() { 
    super.onResume(); 
    myLocationOverlay.enableMyLocation(); 
    myLocationOverlay.enableCompass(); 
    } 

    @Override 
    protected void onPause() { 
    super.onPause(); 
    myLocationOverlay.disableMyLocation(); 
    myLocationOverlay.disableCompass(); 
    } 

나는 또한 MD5 지문과 유효한 API 키를 생성합니다. 하지만 현재 위치 만 보여 주려고 할 때 이름이 나에게 전달되는 위치를 표시해야합니다.
참조 또는 힌트를 제공해주십시오.
미리 감사드립니다.

+0

어디를 가리키고 있습니까? 푸네 (Pune) 또는 완전히 다른 위치의 어딘가에 있습니까? –

+0

어딘가에 푸네 ... –

답변

0

내 에뮬레이터에서 비슷한 문제가 발생했습니다. 장치에서 작동하지 않게 될 수도 있습니다. (아직 장치에서는 테스트하지 않았습니다!) 그래서이 문제를 해결하려면 다른 원시 API를 사용하여 geopoint에 주소를 변환하십시오 ...

그래서 현재 코드를 삭제하거나 변경하지 말고 (내게 잘 보이고 장치 시나리오에서 작동하는 것으로 끝날 수도 있음) 누가이 메서드를 호출 할 것인가? catch (IOException) 절. 내가 쓰고있어 tryAlternative()

private void tryAltesrnative(String address) { 

    String strCompleteAddress = address.replaceAll(" ", "%20"); 
    String streetName = strCompleteAddress.substring(strCompleteAddress.indexOf(",")+1); 
    String cityName = streetName.substring(streetName.indexOf(",")+1); 
    String addressToToast = ""; 


    HttpGet httpGet = new HttpGet("http://maps.google." + "com/maps/api/geocode/json?address=" + address + "ka&sensor=false"); 


    HttpClient client = new DefaultHttpClient(); 
    HttpResponse response; 
    StringBuilder stringBuilder = new StringBuilder(); 

    try { 
     response = client.execute(httpGet); 
     HttpEntity entity = response.getEntity(); 
     InputStream stream = entity.getContent(); 
     int b; 
     while ((b = stream.read()) != -1) { 
      stringBuilder.append((char) b); 
     } 
    } catch (ClientProtocolException e) { 
    } catch (IOException e) { 
    } 

    JSONObject jsonObject = new JSONObject(); 
    try { 
     jsonObject = new JSONObject(stringBuilder.toString()); 
    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 



    Double lon = new Double(0); 
    Double lat = new Double(0); 

    try { 

     lon = ((JSONArray)jsonObject.get("results")).getJSONObject(0) 
      .getJSONObject("geometry").getJSONObject("location") 
      .getDouble("lng"); 

     lat = ((JSONArray)jsonObject.get("results")).getJSONObject(0) 
      .getJSONObject("geometry").getJSONObject("location") 
      .getDouble("lat"); 

    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 








    point = new GeoPoint((int) (lat * 1E6), (int) (lon * 1E6)); 

    mapController.animateTo(point); 
    mapController.setZoom(12); 

    List<Overlay> mapOverlays = mapView.getOverlays(); 
    Drawable drawable = this.getResources().getDrawable(R.drawable.mark_green); 
    AddItemizedOverlay itemizedOverlay = new AddItemizedOverlay(drawable, this); 

    OverlayItem overlayitem = new OverlayItem(point, "Customer", addressToToast); 

    itemizedOverlay.addOverlay(overlayitem); 
    mapOverlays.add(itemizedOverlay); 
    mapView.invalidate(); 

    Toast.makeText(this, "Lat: " + point.getLatitudeE6() +", Lon: "+ point.getLongitudeE6() + "Address: " + addressToToast, Toast.LENGTH_LONG).show(); 

} 
0

당신은 5 번째 또는 마지막 위치의 위도와 경도 (더 적합) 1 값과 완전히 다른 것을 기회가있다 5 개 주소

List<Address> addresses = geoCoder.getFromLocationName(value, 5); 

의 최대 요구되기 때문에.

마지막 위치에 애니메이션을 적용하려고합니다. 첫 번째 위치에 애니메이션을 적용 해보십시오.

관련 문제