2014-11-04 80 views
0

我使用google maps 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)); 

} 

我運行沒有錯誤的程序。但我的問題是該行

getMap().setMyLocationEnabled(true); 

給了我在地圖上的藍色指針用戶當前位置和我手動用戶CURENT位置添加一個標記(AS上面的代碼中)。我的標記和藍色指針必須因爲他們描述相同的位置(相同的經度和緯度),但他們不是。運行我的代碼後,輸出看起來像這個圖像 https://www.dropbox.com/s/argxzb1fnarzie4/Screenshot_2014-11-05-02-43-06.png?dl=0 我的問題是根據我的代碼他們需要相同的位置,但爲什麼不呢?我怎樣才能在藍色指針的相同位置添加標記。 Thnanks提前

+0

會發生什麼OnLocationChanged(位置定位)//getMap().addMarker(markerOptions)爲什麼這個評論; – 2014-11-04 21:32:17

回答

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 2014-11-05 06:28:04

0

我認爲它是因爲你的currentLocation是最後一個已知位置。看看你是否設置currentLocation到

Currentloc = getMap().getMyLocation() 
+0

兄弟當我設置這個我的應用程序崩潰,因爲空指針異常@大衛Zafrani – jubayer 2014-11-05 06:29:16

+0

你正在執行從擴展調用的類是什麼類? – zafrani 2014-11-05 14:19:40

相關問題