I am trying to show my current location and store the location to Firebase and to place a marker on my current location.
i searched in google and even stack overflow t couldn't find anything related to my problem.
Also i read googles documentation on its maps sdk but didn't understand it.
LocationCallback mLocationCallback = new LocationCallback() { //NEW onLocation changed
#Override
public void onLocationResult(LocationResult locationResult) {
for (Location location : locationResult.getLocations()) {
mLastLocation = location;
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(11));//1-21
}
}
};
I expect to see a blue dot in my app but instead only see the map without any markers or even without my current location
public fun placeMarkerOnMap(location: LatLng, drawableResInt: Int? = null): Marker? {
val markerOptions = MarkerOptions().position(location)
val marker = googleMap?.addMarker(markerOptions)
if (drawableResInt != null) {
marker?.setIcon(BitmapDescriptorFactory.fromResource(drawableResInt))
}
return marker
}
Your code miss the part of adding marker.You can add marker to particular LatLng with/without custom image.Then only, move the camera.
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng))
Add Marker By doing following
double latitude = location.getLatitude();
double longitude = location.getLongitude());
LatLng latlng= new LatLng(latitude, longitude); ////your lat lng
mMap.addMarker(new MarkerOptions().position(latlng));
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(posisiabsen, 65);
mMap.animateCamera(cameraUpdate);
Related
This question already has answers here:
My current location always returns null. How can I fix this?
(7 answers)
Closed 7 years ago.
Hi I need some help with this, I have already looked and try many different things. I'm trying to get my current location on android but since the location comes back null my app crashes. I really need help with this please.
I don't know if I'm being clear in here but everytime I call getLastKnownlocation it comes back null, so when I try to get the double lat= location.getLatitude() same for the longitude it won't return anything and there is where my app crashes.
Help...
Here a piece of code
mMap.setMyLocationEnabled(true);
LocationManager locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
double lat = location.getLatitude();
double lng = location.getLongitude();
LatLng latLng = new LatLng(lat, lng);
at latitude is where it stops.
Do you retrieve the GoogleMap object from the fragment before calling those functions?
Something like:
//get map fragment from the static layout (in this case it has id = "map")
MapFragment mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map);
//get GoogleMap object, then you can start using it, for example enabling the location
map = mapFragment.getMap();
map.setMyLocationEnabled(true);
Thank you all for your help I appreciate it, I have already solved my problem.
Here a piece of code that helped me get ti done:
//I needed to have this line before everything else for me to get my current
//location from getLastKnownLocation method.
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker").snippet("Snippet"));
//And the all the codes I had before
mMap.setMyLocationEnabled(true);
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
double lat = location.getLatitude();
double lng = location.getLongitude();
LatLng latLng = new LatLng(lat, lng);
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(14));
mMap.addMarker(new MarkerOptions()
.position(new LatLng(lat, lng))
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
.title("Here"));
What I want to do is just show driving routes to my Fragment Map when the Marker is Clicked.
This Tutorial is my source.
Right now the route only shows on OTHER app not in the myactivity layout. What I want to do is show the driving direction route in my fragment
Here is my MainActivity.Class
public class MainActivity extends ActionBarActivity implements GoogleMap.OnMarkerClickListener {
private LatLng Mark_Kantor_Gubernur = new LatLng(-0.9376155, 100.3600001);
private LatLng Mark_Bappeda_prov_Sumbar = new LatLng(-0.913884, 100.359176);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Getting Map From Google
SupportMapFragment supportMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.googleMap);
googleMap = supportMapFragment.getMap();
googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setCompassEnabled(true);
googleMap.getUiSettings().setZoomControlsEnabled(true);
googleMap.setTrafficEnabled(true);
//Getting Location Based on Fused Location(GPS & Network)
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String bestProvider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(bestProvider);
double latitude = location.getLatitude();
double longitude = location.getLongitude();
LatLng latLng = new LatLng(latitude, longitude);
//Handle map when opened on first time
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
googleMap.animateCamera(CameraUpdateFactory.zoomTo(13));
//Setting UP Marker
googleMap.addMarker(new MarkerOptions().position(Mark_Kantor_Gubernur)
.title("Kantor Gubernur")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_cp)));
googleMap.addMarker(new MarkerOptions().position(Mark_Bappeda_prov_Sumbar)
.title("Kantor Bappeda SUMBAR")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.marker_cp)));
TextView locationTV = (TextView) findViewById(R.id.tvLatLongLocation);
locationTV.setText("Latitude:" + latitude + ",Longitude:" + longitude);
//Setting onclick marker & Direction to Marker
googleMap.setOnMarkerClickListener(this);
}
#Override
public boolean onMarkerClick(Marker marker) {
//you can handle marker click from here
try {
StringBuilder stringBuilder = new StringBuilder();
String daddr = (String.valueOf(marker.getPosition().latitude)+","+String.valueOf(marker.getPosition().longitude));
stringBuilder.append("http://maps.google.com/maps?f=d&hl=en");
stringBuilder.append("&saddr="+String.valueOf(googleMap.getMyLocation().getLatitude())+","
+String.valueOf(googleMap.getMyLocation().getLongitude()));
stringBuilder.append("&daddr="+daddr);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(stringBuilder.toString()));
startActivity(intent);
}catch (Exception ee)
{
Toast.makeText(getApplicationContext(), "Lokasi Saat Ini Belum Didapatkan, Coba Nyalakan GPS, Keluar Ruangan dan Tunggu Beberapa Saat", Toast.LENGTH_LONG).show();
}
return false;
}
I have looked at J2ME/Android/BlackBerry - Driving directions, Route Between Two Locations, Draw Route On Google Map but this is showing just straight lines between the users location and destination, what I want is driving route.
If I add this polyline code:
Polyline polyline = googleMap.addPolyline(new PolylineOptions().add(new LatLng(googleMap.getMyLocation().getLatitude(), googleMap.getMyLocation().getLongitude()),new LatLng(marker.getPosition().latitude, marker.getPosition().longitude)).width(2).color(Color.RED).geodesic(true));
It just shows straight lines in a Google map fragment
Note: Please include comments and an explanation on answer code, I'm very new in android and still learning.
Selector elements should be placed in res\drawable. Right click on 'drawable', select 'new' then select 'Drawable resource file'. Name the file and you should be good to go.
I want to stop the LocationListener once it has the current location and zoomed to it.
Can you please help a newbie?
Here's my code:
#Override
public void onMyLocationChange(Location 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);
// Showing the current location in Google Map
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 15));
}
Set the map location listener to null to turn it off.
mMap.setOnMyLocationChangeListener(null);
Sadly,the above solution did not work for me,
I had to add:
mMap.setMyLocationEnabled(false);
i want to make a program to calculate the distance between some places to my current location, but my googleMap.getMyLocation(); doesnt work properly.
googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setCompassEnabled(false);
mylocation = googleMap.getMyLocation();
direction = new GMapV2Direction();
LatLng from, to;
from = new LatLng(-7.26071409, 112.80674726);
for(int i=0; i<lat.length; i++){
to = new LatLng(lat[i], lon[i]);
doc = direksi.getDocument(from, to, GMapV2Direction.MODE_DRIVING);
distance[i] = (double)direction.getDistanceValue(doc) / 1000;
}
i saved the latitude and longitude of some places in lat[] and lon[].
LatLng 'from' is mylocation and 'to' is my destination places.
the problem appear when i change
from = new LatLng(-7.26071409, 112.80674726);
to
from = new LatLng(mylocation.getLatitude(), mylocation.getLongitude());
i want to do the calculation without opening the googlemaps. the google maps will appear when i touch the map button as pop up window. so the calculation will happen without opening googlemap
please help me
Try this...I hope this will be help to you
LocationManager locationManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
Location location = locationManager.getLastKnownLocation(locationManager
.getBestProvider(criteria, false));
double latitude = location.getLatitude();
double longitude = location.getLongitude();
This is my code for you, I hope this help you... edit it if you think, it isn't with correct sintaxis...
This work for android 4.x and 5.x and 6.x, I not test this in android 7
//I use this like global var to edit or get informacion about google map
GoogleMap gMap;
#Override
public void onMapReady(GoogleMap googleMap) {
gMap = googleMap;
if (ActivityCompat.checkSelfPermission(Activity_Map.this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(Activity_Map.this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(Activity_Map.this,new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 1);
}else{
if(!gMap.isMyLocationEnabled())
gMap.setMyLocationEnabled(true);
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location myLocation = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (myLocation == null) {
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
String provider = lm.getBestProvider(criteria, true);
myLocation = lm.getLastKnownLocation(provider);
}
if(myLocation!=null){
LatLng userLocation = new LatLng(myLocation.getLatitude(), myLocation.getLongitude());
gMap.animateCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 14), 1500, null);
}
}
}
getMyLocation() => this method is deprecated. You need to use LocationClient instead.
You can find informations about LocationClient and how to get current location here
I already implement Google map to find location and my current position..Position and my current location shows well.But my current location indicates by blue dot(default),I want to customize.I know it will done by marker but not working it...
here is my code..
private void initilizeMap() {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
googleMap.getUiSettings().setCompassEnabled(false);
googleMap.getUiSettings().setRotateGesturesEnabled(true);
googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Target Location + "+ latitude+" +"+longitude);
marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW));
// adding marker
googleMap.addMarker(marker);
Location myLocation = googleMap.getMyLocation();
if(myLocation!=null)
{
double dLatitude = myLocation.getLatitude();
double dLongitude = myLocation.getLongitude();
Log.i("APPLICATION"," : "+dLatitude);
Log.i("APPLICATION"," : "+dLongitude);
googleMap.addMarker(new MarkerOptions().position(
new LatLng(dLatitude, dLongitude)).title("My Location").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
// mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(dLatitude, dLongitude), 8));*/
}
else
{
Toast.makeText(this, "Unable to fetch the current location", Toast.LENGTH_SHORT);
}
}
You set your new marker's position when you initialize your map. But location provider will update your location for finding your real location. So you should track your location and change marker location on every update.