2014-11-04 76 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

相关问题