谷歌地图我得到当前的位置当我改变位置标记不会在谷歌地图

问题描述:

这里移动是我的代码:谷歌地图我得到当前的位置当我改变位置标记不会在谷歌地图

public class MapViewFragment extends Fragment implements OnMapReadyCallback { 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.maps, container, false); 

    Context context =this.getContext(); 
    GPSService mGPSService = new GPSService(context); 
    mGPSService.getLocation(); 

    if (mGPSService.isLocationAvailable == false) { 

    } else { 

     // Getting location co-ordinates 
     lat1 = mGPSService.getLatitude(); 
     lng1 = mGPSService.getLongitude(); 

    } 

    mMapView.onCreate(savedInstanceState); 
    ActivityCompat.requestPermissions(getActivity(),new String[]{ 
      android.Manifest.permission.ACCESS_FINE_LOCATION}, 1); 
    call.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View view) { 
      call(); 
     } 

    }); 
    mMapView.getMapAsync(this); //this is important 
       } 
    });*/ 

    } 
    return v; 
}  
    @Override 

public void onMapReady(GoogleMap googleMap) { 
    mGoogleMap = googleMap; 
    mGoogleMap.getUiSettings().setZoomControlsEnabled(true); 

    m1 = googleMap.addMarker(new MarkerOptions() 
      .position(new LatLng(lat1,lng1)) 
      .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)) 
      .anchor(0.5f, 0.5f) 
      .title("Title1") 
      .snippet("Snippet1")); 

@Override 
public void onResume() { 
    super.onResume(); 
    mMapView.onResume(); 
} 

@Override 
public void onPause() { 
    super.onPause(); 
    mMapView.onPause(); 
} 



@Override 
public void onDestroy() { 
    super.onDestroy(); 
    mMapView.onDestroy(); 
} 

@Override 
public void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 
mMapView.onSaveInstanceState(outState); 
} 

    @Override 
    public void onLowMemory() { 
     super.onLowMemory(); 
     mMapView.onLowMemory(); 
    } 
} 

在节目我获取当前位置经纬度和lang.When我移到其他位置标记不会改变。每当我移动的位置不得不改变。 当前位置值iam每当我移动位置值时都不会改变。要实现任何方法改变位置。

+0

移动到你的意思是你真正移动,并期待在位置其他位置?或者你的意思是用mapview移动到其他位置? –

+0

我可以从代码中获取当前位置。如何在谷歌地图上连续移动标记与谷歌默认当前位置标记相同android – Jayapriya

+0

感谢您的回复 – Jayapriya

我假设你已经创建了一个标记来指示你的当前位置。像这样 Marker currentLocation;

现在您需要实现LocationListener的,并在onLocationChanged回调更新的MapView

@Override 
public void onLocationChanged(Location location) { 
if(currentLocation != null){ 
      currentLocation.remove(); 
     } 

TextView tvLocation = (TextView) findViewById(R.id.tv_location); 

// Getting latitude of the current location 
double latitude = location.getLatitude(); 

// Getting longitude of the current location 
double longitude = location.getLongitude(); 

// Creating a LatLng object for the current location 
LatLng latLng = new LatLng(latitude, longitude); 
currentLocation = googleMap.addMarker(new MarkerOptions().position(latLng))); 
// Showing the current location in Google Map 
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 

// Zoom in the Google Map 
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15)); 
} 
+0

感谢我将尝试并说出结果 – Jayapriya

+0

Soory它不适用于me.default谷歌地图从片段中的当前位置移动不活动类很多例子都在活动中。就像我想创建应用程序请帮助我 – Jayapriya

+0

是的,你是在mapfragment所有你需要做的就是链接一个监听器。键入mMapView并设置位置更改侦听器那里... https://developers.google.com/android/reference/com/google/android/gms/maps/GoogleMap.OnMyLocationChangeListener like mMapView.OnMyLocationChangeListener(this);实现OnMapReadyCallback,OnMyLocationChangeListener ,我给你一个你需要覆盖的整个方法。 –