2014-11-04 2 views
0

Google지도 api v2를 사용하여 맞춤형 Android지도를 만들고 있습니다. 나는이 사용자의 현재 location.my 코드에 마커를 넣어 원 :현재 위치에 마커를 추가하는 데 문제가 있습니다.

// Enabling MyLocation in Google Map 
    getMap().setMyLocationEnabled(true); 

    // BY THIS YOU CAN CHANGE MAP TYPE 
    // mGoogleMap.setMapType(mGoogleMap.MAP_TYPE_SATELLITE); 

    try { 
     locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 

     // getting GPS status 
     isGPSEnabled = locationManager 
       .isProviderEnabled(LocationManager.GPS_PROVIDER); 

     // getting network status 
     isNetworkEnabled = locationManager 
       .isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

     if (!isGPSEnabled && !isNetworkEnabled) { 
      // no network provider is enabled 
     } else { 
      this.canGetLocation = true; 
      // First get location from Network Provider 
      if (isNetworkEnabled) { 
       locationManager.requestLocationUpdates(
         LocationManager.NETWORK_PROVIDER, 
         MIN_TIME_BW_UPDATES, 
         MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
       Log.d("Network", "Network"); 
       if (locationManager != null) { 
        Currentloc = locationManager 
          .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
        if (Currentloc != null) { 
         onLocationChanged(Currentloc); 
         latitude = Currentloc.getLatitude(); 
         longitude = Currentloc.getLongitude(); 
        } 
       } 
      } 
      // if GPS Enabled get lat/long using GPS Services 
      if (isGPSEnabled) { 
       if (Currentloc == null) { 
        locationManager.requestLocationUpdates(
          LocationManager.GPS_PROVIDER, 
          MIN_TIME_BW_UPDATES, 
          MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
        Log.d("GPS Enabled", "GPS Enabled"); 
        if (locationManager != null) { 
         Currentloc = locationManager 
           .getLastKnownLocation(LocationManager.GPS_PROVIDER); 
         if (Currentloc != null) { 
          onLocationChanged(Currentloc); 
          latitude = Currentloc.getLatitude(); 
          longitude = Currentloc.getLongitude(); 
         } 
        } 
       } 
      } 
     } 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    markerOptions = new MarkerOptions(); 

    // double lat = Currentloc.getLatitude(); 
    // double lng = Currentloc.getLongitude(); 

    LatLng latLng = new LatLng(latitude, longitude); 

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

    // Setting the title for the marker. 
    // This will be displayed on taping the marker 
    markerOptions.title("ADD IMAGE?"); 
    // markerOptions.snippet("ADD IMAGE?"); 

    // Placing a marker on the touched position 
    getMap().addMarker(markerOptions); 
    getMap().setOnMarkerClickListener((OnMarkerClickListener) this); 

코드를 OnLocationChanged 오류가없는 실행

public void onLocationChanged(Location location) { 
    // TODO Auto-generated method stub 
    mLatitude = location.getLatitude(); 
    mLongitude = location.getLongitude(); 
    // LatLng latLng = new LatLng(mLatitude, mLongitude); 
    //markerOptions.notify(); 

    //getMap().addMarker(markerOptions); 
    getMap().moveCamera(
      CameraUpdateFactory.newLatLng(new LatLng(
        location.getLatitude(), location.getLongitude()))); 
    getMap().animateCamera(CameraUpdateFactory.zoomTo(12)); 

} 

내 프로그램입니다. 사용자의 현재 위치에 나에게지도에 파란색 포인터를 주어진 그리고 난 수동 (위의 코드 AS) 사용자 curent 위치에 마커를 추가

getMap().setMyLocationEnabled(true); 

하지만 내 문제는 누군 데 마커와 블루 포인터에이 라인을입니다 그들은 같은 loc (동일한 위도와 경도)를 설명하기 때문에 같은 위치가됩니다. 그러나 내 코드를 실행하면 출력이이 이미지처럼 보입니다. https://www.dropbox.com/s/argxzb1fnarzie4/Screenshot_2014-11-05-02-43-06.png?dl=0 내 질문에 따르면 내 위치는 동일하지만 왜 그럴 필요가없는 것입니까? 파란색 포인터의 동일한 위치에 마커를 추가하려면 어떻게해야합니까? Thnanks 사전에 내가 노력 할게요

+0

왜 이것을 OnLocationChanged (위치 정보) //getMap().addMarker(markerOptions)에 주석으로 만드세요? –

답변

0

우선은 아래) onLocationChanged (내부 코드의 라인을 이동

markerOptions = new MarkerOptions(); 

// double lat = Currentloc.getLatitude(); 
// double lng = Currentloc.getLongitude(); 

LatLng latLng = new LatLng(latitude, longitude); 

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

// Setting the title for the marker. 
// This will be displayed on taping the marker 
markerOptions.title("ADD IMAGE?"); 
// markerOptions.snippet("ADD IMAGE?"); 

// Placing a marker on the touched position 
getMap().addMarker(markerOptions); 
+0

시도해도 동일합니다. 변경하지 않습니다. @jucas – jubayer

0

내 생각은 당신의 currentLocation가 마지막으로 알려진 위치이기 때문이다. 현재 위치를

으로 설정하면 어떻게되는지 확인하십시오.
Currentloc = getMap().getMyLocation() 
+0

형 (null 포인터 예외로 인해 내 앱이 다운 됨) @David Zafrani – jubayer

+0

전화를 연장하는 클래스는 어떤 클래스입니까? – zafrani

관련 문제