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.
Related
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);
I have already done that is google map on set marker in current location(Current Lat/Long).Now i want to set marker from users current location to 1 miles,i was stuck in this how to solve this?.
for(LatLng point1 : new LatLng[]{point1, point2, point3, point4})
{
targetLocation.setLatitude(point1.latitude);
targetLocation.setLongitude(point1.longitude);
if(currentBestLocation.distanceTo(targetLocation) < 800)
{}
In above code the target locations takes point4 latitude longitude only.
try with this code
GPSTracker gps;
gps = new GPSTracker(SearchLocation.this);
if (gps.canGetLocation()) {
usersearchlat = gps.getLatitude();
usersearchlong = gps.getLongitude();
} else {
gps.showSettingsAlert();
}
or you can use some other code for lat,long and use this code-
mMapView.onResume();// needed to get the map to display immediately
try {
MapsInitializer.initialize(getApplicationContext()
.getApplicationContext());
googleMap = mMapView.getMap();
googleMap.setMyLocationEnabled(true);
googleMap.setMapType(googleMap.MAP_TYPE_HYBRID);
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
googleMap.getUiSettings().setCompassEnabled(true);
googleMap.getUiSettings().setZoomControlsEnabled(true);
googleMap.getUiSettings().setRotateGesturesEnabled(true);
} catch (Exception e) {
e.printStackTrace();
}
googleMap.addMarker(marker);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(usersearchlat, usersearchlong))
.zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
add some perditions like these :-
<uses-permission android:name="android.permisssion.ACCESS_FINE_LOCATION">
</uses-permission>
<uses-permission
android:name="android.permisssion.ACCESS_COARSE_LOCATION"></uses-
permission>
<uses-permission android:name="android.permisssion.INTERNET"/>
Access_Coarse_location.
I use Google Map Android API v2 in my app and I have a little problem as in the picture below but have no ideas how to fix it. I set marker to current location of device but its now in the center of circle. I want to set marker to the center of the circle. Is it possible or not?
Thanks for any help!
Code that I used:
// Showing status
if(status!= ConnectionResult.SUCCESS){ // Google Play Services are not available
int requestCode = 10;
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
dialog.show();
}else { // Google Play Services are available
// Getting reference to the SupportMapFragment of activity_location.xml
SupportMapFragment mSupportMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
// Getting GoogleMap object from the fragment
mGoogleMap = mSupportMapFragment.getMap();
// Enabling MyLocation Layer of Google Map
mGoogleMap.setMyLocationEnabled(true);
// Getting LocationManager object from System Service LOCATION_SERVICE
LocationManager mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// Creating a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Getting the name of the best provider
String provider = mLocationManager.getBestProvider(criteria, true);
// Getting Current Location
Location location = mLocationManager.getLastKnownLocation(provider);
if(location!=null){
onLocationChanged(location);
}
mLocationManager.requestLocationUpdates(provider, 20000, 0, this);
// 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 mLatLng = new LatLng(latitude, longitude);
//Add Marker to current location of devise
mGoogleMap.addMarker(new MarkerOptions()
.position(mLatLng)
.title("Geolocation system")
.snippet("Your last current location which was available!"));
// .icon(BitmapDescriptorFactory.fromResource(R.drawable.icon_location)));
// Showing the current location in Google Map
mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(mLatLng));
// Show Zoom buttons
mGoogleMap.getUiSettings().setZoomControlsEnabled(true);
// Turns on 3D buildings
mGoogleMap.setBuildingsEnabled(true);
// Zoom in the Google Map
mGoogleMap.animateCamera(CameraUpdateFactory.zoomTo(14));
You are getting your location using GPS and network. these are not giving your correct location and you are using:-
mGoogleMap.setMyLocationEnabled(true);
this showing your location using google map api. so the loaction by you and google are different .so circle and marker are showing different location.
so you can use google's Fuesed Location client for this
Visit this for more help.
https://developer.android.com/reference/com/google/android/gms/location/FusedLocationProviderApi.html
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);