I'm trying to control Google map camera like this
private void setUpMap() {
Log.e(LOG_TAG, "in setup method");
mMap.setMyLocationEnabled(true);
LatLng startingPoint = new LatLng(129.13381, 129.10372);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(startingPoint, 16));
Log.e(LOG_TAG, "in Setup method" + (mMapFragment == null));
}
LogCat prints
"in setup method"
"in setup method false"
2 log is shown it means mMap.moveCamera(...) is called
setUpMap() call from here
private void setUpMapIfNeeded() {
mMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentByTag(MFragment.TAG);
if (mMapFragment != null) {
mMapFragment.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
setUpMap();
}
});
}
}
try this: hope it will work.
private void setUpMap() {
Log.e(LOG_TAG, "in setup method");
mMap.setMyLocationEnabled(true);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(latitude, longitude)).zoom(15).build();
mMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
Log.e(LOG_TAG, "in Setup method" + (mMapFragment == null));
}
Your coordinates specified at LatLng startingPoint = new LatLng(129.13381, 129.10372); seems to be a bit off. In more detail, the maximum latitude is 90 degrees, which is the north pole (-90 is the south pole).
The result of this will be that the camera will not move to a position which is invalid.
Try using coordinates from a known location, such as LatLng startingPoint = new LatLng(55.70, 13.19); which will give you the position of Lund, Sweden.
So basically, revise the latitude and longitude coordinates for your position.
Related
I am using the following method to move the camera:
private void moveCamera(LatLng latLng, float zoom, GoogleMap map) {
Log.d(TAG, "moveCamera: moving the camera to: lat: " + latLng.latitude + ", lng: " + latLng.longitude);
map.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, zoom));
}
When I am using it inside OnMapReady like this:
private void initMapHome() {
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.harta_adauga_adresa_acasa);
mapFragment.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap googleMap) {
Log.d(TAG, "onMapReady: map is readyyyyyyyyyyyyyyyyyyy");
gmap = googleMap;
if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
gmap.setMyLocationEnabled(true);
gmap.getUiSettings().setMyLocationButtonEnabled(true);
gmap.getUiSettings().setCompassEnabled(true);
gmap.getUiSettings().setMapToolbarEnabled(false);
gmap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
return false;
}
});
gmap.setOnMapClickListener(latLng -> {
gmap.clear();
MarkerOptions options = new MarkerOptions()
.position(latLng)
.title(adresa);
gmap.addMarker(options).showInfoWindow();
});
moveCamera(pozitie_curenta, DEFAULT_ZOOM, gmap);
}
});
}
The camera animates in a nice manner to the selected position, the pozitie_curenta LatLng created somewhere else in my code. Now, I have a AutoCompleteTextView that returns the places (I am using the one created by Mukesh Solanki from github), and I am looking to move the camera on the selected place from the AutoCompleteTextView. I have the following code:
adresa_home.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Place place = (Place) parent.getItemAtPosition(position);
adresa_home.setText(place.getDescription());
placesApi.fetchPlaceDetails(place.getId(), new OnPlacesDetailsListener() {
#Override
public void onPlaceDetailsFetched(PlaceDetails placeDetails) {
latitudine_acasa = placeDetails.getLat();
longitudine_acasa = placeDetails.getLng();
updateHartaHome(latitudine_acasa, longitudine_acasa, place.getDescription());
}
#Override
public void onError(String s) {
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
}
}
);
}
});
The place is fetched correctly, and I get the latitude and longitude correctly. The method updateHartaHome is looking like this:
private void updateHartaHome(double lat, double lng, String title) {
acasa = new LatLng(lat, lng);
MarkerOptions options = new MarkerOptions()
.position(pozitie_curenta)
.title(title);
gmap.addMarker(options).showInfoWindow();
moveCamera(acasa, DEFAULT_ZOOM, null, gmap);
}
Debugging the code I've come to the conclusion that the problem it's with the moveCamera from the updateHartaHome which is never called. Setting a breakpoint on the line gmap.addMarker(options).showInfoWindow(); gets me the following:
So, eveything looks fine, but the map doesn't update and, it also doesn't add the marker. Setting the breakpoint on the moveCamera line, it never gets accesed. Any help would be appreciated, I've been going mad over this issue. Thanks!
Please try this once inside your onMapReady method
CameraPosition cameraPosition = CameraPosition.builder()
.target(new LatLng(placeLatitude, placeLongitude))
.zoom(16)
.bearing(0)
.tilt(45)
.build();
mGoogleMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
where mGoogleMap is defined as
mGoogleMap = googleMap;
mGoogleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
mGoogleMap.getUiSettings().setAllGesturesEnabled(true);
if you want to add Marker do this before moving the camera
mGoogleMap.addMarker(new MarkerOptions().position(new LatLng(placeLatitude, placeLongitude)));
EDIT:
If you want to click on the map and move the camera
mGoogleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
#Override
public void onMapClick(LatLng latLng) {
mGoogleMap.clear();
mGoogleMap.addMarker(new MarkerOptions().position(new LatLng(latLng.latitude, latLng.longitude)));
CameraPosition cameraPosition = CameraPosition.builder()
.target(new LatLng(latLng.latitude, latLng.longitude))
.zoom(16)
.bearing(0)
.tilt(45)
.build();
mGoogleMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
});
I work on a project and I want to show road traffic and time estimates on the map route, like the Googlemap.
.![Like this image
]1
if (direction.isOK()) {
Route route = direction.getRouteList().get(0);
ArrayList<LatLng> directionPositionList = route.getLegList().get(0).getDirectionPoint();
polyline = mGoogleMap.addPolyline(DirectionConverter.createPolyline(mActivity, directionPositionList, 5
, Color.BLUE));
setCameraWithCoordinationBounds(route);
Leg leg = route.getLegList().get(0);
Info distanceInfo = leg.getDistance();
Info durationInfo = leg.getDuration();
String distance = distanceInfo.getText();
String duration = durationInfo.getText();
}
If you are using google maps sdk, you can enable traffic details once app has your location.
#Override
public void onMyLocationChange(Location arg0) {
mMap.addMarker(new MarkerOptions().position(new LatLng(arg0.getLatitude(), arg0.getLongitude())).title("Here"));
//load the traffic now
googleMap.setTrafficEnabled(true);
}
});
You may also try the following code. It initializes the map properly then sets traffic data after detecting your current location.
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
mMap.setMyLocationEnabled(true);
// Check if we were successful in obtaining the map.
if (mMap != null) {
mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
#Override
public void onMyLocationChange(Location arg0) {
// TODO Auto-generated method stub
mMap.addMarker(new MarkerOptions().position(new LatLng(arg0.getLatitude(), arg0.getLongitude())).title("It's Me!"));
//load the traffic now
googleMap.setTrafficEnabled(true);
}
});
}
}
}
I do want to get current location with GoogleApiClient with this code below,
#Override
public void onConnected(#Nullable Bundle bundle) {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if(mLastLocation != null)
{
currentLat = mLastLocation.getLatitude();
currentLon = mLastLocation.getLongitude();
}else
{
Toast.makeText(getApplicationContext(), "Cannot get lat and lon", Toast.LENGTH_SHORT).show();
}
}
then after that i do want to put marker on current location, my problem is mLastlocation still null
#Override
public void onMapReady(GoogleMap googleMap) {
dGoogleMap = googleMap;
if(mLastLocation != null)
{
MarkerOptions marker = new MarkerOptions()
.position(new LatLng(currentLat, currentLon))
.title("My Current Location");
dGoogleMap.addMarker(marker);
dGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(currentLat, currentLon), 16));
}
}
Or this is my fault missunderstand the flow of async, or just my poor logic needs to be improved.
Put below line in your onMapReady method
dGoogleMap.setMyLocationEnabled(true);
Current location pointer is jumping i dump to resolve the issue.I want to stop jumping the current pointer pointer.I want to show pointer without jumping situation.
I tried 4 days but not done this Task.
private boolean initMap() {
if (mMap == null && FirstTimeMapIniciate == 0) {
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mMap = mapFragment.getMap();
mMap.setMyLocationEnabled(true);
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
return (mMap != null);
}
Here is OnLocationChange Code
#Override
public void onConnected(Bundle bundle) {
Toast.makeText(this, "Ready to map!", Toast.LENGTH_SHORT).show();
mListener = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
latitude = location.getLatitude();
longitude = location.getLongitude();
LatLng latLng1 = new LatLng(latitude, longitude);
MarkerOptions mp = new MarkerOptions();
mp = new MarkerOptions();
mp.position(new LatLng(location.getLatitude(),
location.getLongitude()));
mMap.setMyLocationEnabled(true);
// mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
Toast.makeText(MainActivity.this, "Location : " + location.getLatitude() + ", " + location.getLongitude(), Toast.LENGTH_LONG).show();
if (FirstTimeMapIniciate == 0) {
gotoLocation(location.getLatitude(), location.getLongitude(), 15);
FirstTimeMapIniciate = 1;
}
AppUtill.UniqueId();
new JSONAsyncTask().execute("http://103.8.7/hajjapi/api/GPSLocator/GetLocations");
if (AppStatus.getInstance(getContext()).isOnline()) {
} else {
Toast.makeText(MainActivity.this, "Turn On your WIFI ", Toast.LENGTH_LONG).show();
}
/* if (marker != null) {
marker.remove();
}*/
}
};
LocationRequest request = LocationRequest.create();
request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
request.setInterval(1000);
request.setFastestInterval(1000);
LocationServices.FusedLocationApi.requestLocationUpdates(mLocationClient, request, mListener);
}
There are several problems in your logic as the first you don't need to call setMyLocationUpdate(true) at every location update.
The answer to your question as I'd say you need to implement your own service where you can apply your customized algorithm to filter out those jumps.
OnMapReady gets called before ReadCsvFile() is executed,which fetches the latlng points from the a CSV file.Hence when map is displayed, it doesn't show the current Location.How can i make sure onMapReday is called only after latLng points are fetched from the file? How to prevent getting a null pointer exception at
"googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 17));"
public void onMapReady(final GoogleMap googleMap) {
ReadCsvFile(this);
for (LocationHelperBean locationHelperBean : parameterList) {
latLng=new LatLng(Double.parseDouble(String.valueOf(locationHelperBean.getLatitude())), Double.parseDouble(String.valueOf(locationHelperBean.getLongitude())));
arrayPoints.add(latLng);
}
googleMap.setMyLocationEnabled(true);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 17));
googleMap.addMarker(new MarkerOptions().snippet("latitude:" + latitude + "longitude:" + longitude + "bearing:" + bearing + "accuracy:" + accuracy + "altitude" + altitude + "speed" + speed).position(latLng).draggable(true));
PolylineOptions polylineOptions = new PolylineOptions().add(latLng).width(6).color(Color.RED).geodesic(true);
polylineOptions.addAll(arrayPoints);
map.addPolyline(polylineOptions);
}
If you use MapView you could just call onCreate(null) after you have obtained everything from readCsvFile() to set up the map. Otherwise just get the GoogleMap when it's ready and call setupMap() when readCsvFile() finished like so:
private GoogleMap map;
#Override
public void onMapReady(GoogleMap map) {
this.map = map;
setupMap();
}
private void setupMap() {
if (map != null && latLng != null) {
// Do something with map (e.g. add markers,...)
}
}