2015-08-20 84 views
0

我如何可以重定向我目前的位置,当我打开谷歌地图如何重定向在谷歌地图中的当前位置

这里是我在SetUpMap(代码)

 private void setUpMap() { 
      mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker")); 
      mMap.setMyLocationEnabled(true); 

同时,我如何改变标记?我只在我的位置得到那个蓝色圆圈。我想将其更改为引脚

+0

你是什么意思与重定向?你想改变标记的位置吗? –

+0

是的,因为当我打开谷歌地图时,它显示地图的中心,要显示我的位置,我需要点击按钮。我不想单击按钮我只想显示我的位置,如果我打开谷歌地图 –

+0

我会发布答案 –

回答

1
If you want to open map with your location through any activity then use this code  

Intent intent = new Intent(android.content.Intent.ACTION_VIEW, 
    Uri.parse("http://maps.google.com/maps?saddr=20.344,34.34&daddr=20.5666,45.345")); 
    startActivity(intent); 


If you are using Google map then using location find lat long and pass this lat long in maps object 
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
     Criteria criteria = new Criteria(); 
     String bestProvider = locationManager.getBestProvider(criteria, true); 
     Location location = locationManager.getLastKnownLocation(bestProvider); 
     if (location != null) { 
      onLocationChanged(location); 
     } 
     locationManager.requestLocationUpdates(bestProvider, 20000, 0, this); 
    public void onLocationChanged(Location location) { 
     TextView locationTv = (TextView) findViewById(R.id.latlongLocation); 
     double latitude = location.getLatitude(); 
     double longitude = location.getLongitude(); 
     LatLng latLng = new LatLng(latitude, longitude); 
     googleMap.addMarker(new MarkerOptions().position(latLng)); 
     googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
     googleMap.animateCamera(CameraUpdateFactory.zoomTo(15)); 
     locationTv.setText("Latitude:" + latitude + ", Longitude:" + longitude); 
    } 
0

一旦获取用户的当前位置信息作为你的Map已准备就绪,你需要动画的相机这样

Location location = this.mGoogleMap.getMyLocation(); 

    if (location != null) { 

     LatLng target = new LatLng(location.getLatitude(), location.getLongitude()); 
     CameraPosition position = this.mGoogleMap.getCameraPosition(); 

     Builder builder = new CameraPosition.Builder(); 
     builder.zoom(15); 
     builder.target(target); 

     this.mGoogleMap.animateCamera(CameraUpdateFactory.newCameraPosition(builder.build())); 

     }  

所以,getMyLocation()有获取用户的位置。使用可以使用PROVIDERS来获取位置。您也可以使用GoogleApiClient使用LocationListener来收听位置。在发布之前请阅读Getting the Last Known LocationReceiving Location Updates的文档。

为了让您所在位置的自定义标记有个东西叫addMarker

mGoogleMap.addMarker(new MarkerOptions() 
         .position(new LatLng(location.getLatitude(), location.getLogitude())) 
         .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))); 
+0

我把这段代码放在private void setUpMapIfNeeded()? –

+0

不,你不应该这样做。一旦“地图”准备就绪,您就需要获取用户位置,一旦获取位置,将摄像机设置为特定缩放级别并显示标记。我相信你的尝试是第一次,我会鼓励你先阅读文件。我做了类似这样的事情,在'SplashScreen'中,我在读取后使用'GoogleApiClient'获取位置,我加载'MapActivity',并使用'Map'中的自定义'Marker'获取位置。 –

0

您可以使用此代码尝试,我们可以通过NETWORK_PROVIDER让我们在谷歌地图上的位置。 NETWORK_PROVIDER会从您的WIFI位置或您的网络提供商公司(如IDEA,AIRTEL,VODAPHONE)中挑选您的位置。

try { 
        if (googleMap == null) { 
         googleMap = ((MapFragment) getFragmentManager() 
           .findFragmentById(R.id.map)).getMap(); 
        } 
        googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
        googleMap.setMyLocationEnabled(true); 
        LocationManager location_manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
        LocationListener listner = new getlatlngListner(); 
        location_manager.requestLocationUpdates(
          LocationManager.NETWORK_PROVIDER, 2000, 2000, listner); 
        location_manager 
          .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
0
​​
+0

我还需要使用我的私人无效setUpMap() –

+0

中的代码如果你从位置监听器调用void?或者您需要初始化此方法中的所有内容? –

相关问题