How can I retrieve latitude and longitude of Google Maps data from my database and display it to the user on the map in android ?
I have a database mysql containing some fields as longitude and latitude.
What is the best way to do such?
Is it possible through a Volley library?
If there is an example of this, it is good to me
Yes you can draw stuff in the onMapReady callback. You can create Markers, draw circles, draw polygons.
To add a marker just call:
LatLng point = new LatLng(exampleLat, exampleLng);
mGoogleMap.addMarker(new MarkerOptions().position(point));
Here is an example of how to draw a circle:
CircleOptions circleOptions = new CircleOptions()
.center(new LatLng(lat, lng))
.radius(radius)
.fillColor(Color.RED)
.strokeColor(Color.TRANSPARENT);
Circle circle = mGoogleMap.addCircle(circleOptions);
Here is a Polygon:
PolygonOptions polygonalOptions = new PolygonOptions()
.fillColor(Color.RED)
.strokeColor(Color.TRANSPARENT)
.clickable(true);
for (List<Double> latLng : myPolygon.path) {
LatLng point = new LatLng(latLng.get(0), latLng.get(1));
polygonalOptions.add(point);
}
mGoogleMap.addPolygon(polygonalOptions);
PS: Writing from my phone sorry for any mistakes or formatting issues.
Related
I try to draw circles on the Google Maps API, which are transparent.
CircleOptions circleOptions = new CircleOptions()
.center(new LatLng(lat, lon))
.radius(50.0)
.strokeColor(Color.TRANSPARENT)
.strokeWidth(1)
.fillColor(Color.CYAN); //
But I like to draw them transparently, so that I can see the map behind? How could I do that?
For mGoogleMap as GoogleMap object, with latitude longitude given as
LatLng currentLatLon = new LatLng(lat double value, lng double value);
This is the way to draw circle in google maps
mGoogleMap.addCircle(new CircleOptions()
.center(currentLatLon)
.radius(150)
.strokeColor(Color.RED)
.fillColor(0x220000FF)
.strokeWidth(5)
);
This will add the transparent circle around the marker
Circle mCircle = mMap.addCircle(new CircleOptions()
.center(new LatLng(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude()))
.radius(500)
.strokeWidth(0)
.strokeColor(Color.parseColor("#2271cce7"))
.fillColor(Color.parseColor("#2271cce7")));
For the Transparent Color you can set it to any Color using i.e #2271cce7
In the above the actual color code is #71cce7 and the first two digits of 22 indicate the transparency which can be set to any value to either increase or decrease transparency.
View Sample
I am using the new API(Google Map API V2) for my android application, i have done creating the map and adding markers to it, now my task is to manually create a circle around any of the marker and also i want to provide a functionality to the user that he can increase the radius of that circle accordingly, for this i have given a bar, when user increases that bar the radius of circle will increase and vice versa.
If anybody knows how to do this using Google Map API V2 then please help,
try this code
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tag_map);
// Drawing circle on the map
drawCircle(new LatLng(Latitude, Longitude));
}
private void drawCircle(LatLng point){
// Instantiating CircleOptions to draw a circle around the marker
CircleOptions circleOptions = new CircleOptions();
// Specifying the center of the circle
circleOptions.center(point);
// Radius of the circle
circleOptions.radius(20);
// Border color of the circle
circleOptions.strokeColor(Color.BLACK);
// Fill color of the circle
circleOptions.fillColor(0x30ff0000);
// Border width of the circle
circleOptions.strokeWidth(2);
// Adding the circle to the GoogleMap
googleMap.addCircle(circleOptions);
}
Add the following code in onMapReady() method:
Circle circle = map.addCircle(new CircleOptions()
.center(new LatLng(latitude, longitude))
.radius(10000)
.strokeColor(Color.RED)
.fillColor(Color.BLUE));
where map is the object of GoogleMap class.
Google Maps can use semi-transparent color for the circle.
private var circle: Circle? = null
private fun drawCircle(latitude: Double, longitude: Double, radius: Double) {
val circleOptions = CircleOptions()
.center(LatLng(latitude, longitude))
.radius(radius)
.strokeWidth(1.0f)
.strokeColor(ContextCompat.getColor(context!!, R.color.color1))
.fillColor(ContextCompat.getColor(context!!, R.color.color2))
circle?.remove() // Remove old circle.
circle = googleMap?.addCircle(circleOptions) // Draw new circle.
}
Kotlin version of #Lalit Kushwah
map.addCircle(
CircleOptions()
.center([Your LatLng])
.radius(10000.0)
.strokeColor(Color.RED)
.fillColor(Color.BLUE)
)
CircleOptions circleOptions = new CircleOptions();
circleOptions.center(new LatLng(location.getLatitude(),location.getLongitude()));
circleOptions.radius(700);
circleOptions.fillColor(Color.TRANSPARENT);
circleOptions.strokeWidth(6);
mMap.addCircle(circleOptions);
geocoder = new Geocoder(MapsActivity.this, getDefault());
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(new LatLng(location.getLatitude(), location.getLongitude()));
mMap.addMarker(markerOptions.title(String.valueOf(location)));
try this
In my little app I have a map and i'm parsing an XML from the web, like this:
<place>
<lat>42.827602</lat>
<lon>-1.663957</lon>
<place_name>Place one</place_name>
<snippet>Snippet de place Facebook</snippet>
<thumb>http://www.animage.com/myicon1.png</thumb>
</place>
<place>
<lat>42.830750</lat>
<lon>-1.669064</lon>
<place_name>Place two</place_name>
<snippet>Snippet de place Twitter</snippet>
<thumb>http://www.animage.com/myicon2.png</thumb>
</place>
<place>
<lat>42.825333</lat>
<lon>-1.668232</lon>
<place_name>Place Three</place_name>
<snippet>Snippet de place Skype</snippet>
<thumb>http://www.animage.com/myicon3.png</thumb>
</place>
</response>
every time I read a "place" from the xml, the onPostExecute of my AsyncTask, calls the method that creates a new marker and circle on the map of my application
For example, if the xml has seven "places", the method to create a new marker and a circle is called seven times.
Marker aMarker;
Circle aCircle;
//...
public void createNewMarkerAndCircle() {
//...
aMarker = mMap.addMarker(new MarkerOptions()
.position(new LatLng(dLat, dLon))
.title(nombre_punto)
.snippet(introduccion_punto)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
aCircle = mMap.addCircle(new CircleOptions()
.center(new LatLng(dLat, dLon))
.radius(150)
.strokeColor(Color.RED)
}
//...
public void onPostExecute(String xml) {
xml = stringBuffer.toString();
try {
Document doc = parser.getDomElement(xml);
NodeList nl0 = doc.getElementsByTagName(KEY_PLACE);
Element e = (Element) nl0.item(lap);
theLat = parser.getValue(e, KEY_LATITUDE);
theLon = parser.getValue(e, KEY_LONGITUDE);
//...
createNewMarkerAndCircle();
//...
So far, everything works fine and markers and circles are created on the map.
My purpose is that when the user on the map comes within the radius of one of the circles, get the datas of the marker that is inside.
I show you what I'm doing:
I have a listener of my location that every time it´s updated, check the distance from the current location and radius of the circle.
GoogleMap.OnMyLocationChangeListener myLocationChangeListener = new GoogleMap.OnMyLocationChangeListener() {
#Override
public void onMyLocationChange(Location location) {
LatLng loc = new LatLng(location.getLatitude(), location.getLongitude());
float[] distance = new float[2];
try {
Location.distanceBetween(
location.getLatitude(),
location.getLongitude(),
aCircle.getCenter().latitude,
aCircle.getCenter().longitude, distance);
if (distance[0] > aCircle.getRadius()) {
Log.i("myLogs", "Outside");
} else {
String markerTitle;
markerTitle = aMarker.getTitle();
Log.i("myLogs", "I´ in the circle" + " " + markerTitle);
}
//....
This works fine, but with a big problem.
When I start to walk the route, only detected when I go into the last "place" parsed from xml, and therefore, the last marker and circle created.
I understand what the problem is, the last marker (Marker aMarker;) and circle (Circle aCircle;) created are those with the values assigned ... But i do not know how to fix it.
I would appreciate any help, several days ago I'm looking for solutions, but without success.
Thanks and regards.
More information:
I found this other way , but the problem remains exactly the same:
https://gist.github.com/saxman/5347195
the last marker (Marker aMarker;) and circle (Circle aCircle;)
created are those with the values assigned ... But i do not know how
to fix it.
In order to check the distance against all the points of interests you create you need to hold them in some sort of structure, the most simple being a list, to reference them later in the listener:
Marker aMarker;
Circle aCircle;
// this will hold the circles
List<Circle> allCircles = new ArrayList<Circle>();
// this will hold the markers
List<Marker> allMarkers = new ArrayList<Marker>();
public void createNewMarkerAndCircle() {
aMarker = mMap.addMarker(new MarkerOptions()
.position(new LatLng(dLat, dLon))
.title(nombre_punto)
.snippet(introduccion_punto)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE )));
// store the new marker in the above list
allMarkers.add(aMarker);
aCircle = mMap.addCircle(new CircleOptions()
.center(new LatLng(dLat, dLon))
.radius(150)
.strokeColor(Color.RED)
// store the created circles
allCircles.add(aCircle);
}
// in the LocationListener iterate over all stored circles and check the distances against each one of them
// as you add the circles and markers in the same method you'll have a correspondence between them in the two lists
// another sensible approach would be to create a custom Area class
// to hold both the Marker and the circle in one single place
// so when you find yourself inside a circle the marker will be at the same position in allMarkers
for (int i = 0; i < allCircles.size(); i++) {
Circle c = allCircles.get(i);
Location.distanceBetween(location.getLatitude(), location.getLongitude(), c.getCenter().latitude, c.getCenter().longitude, distance);
if (distance[0] > c.getRadius()) {
Log.i("myLogs", "Outside");
} else {
String markerTitle;
markerTitle = allMarkers.get(i).getTitle();
Log.i("myLogs", "I´ in the circle" + " " + markerTitle);
// you found a circles so you may want to break out of the for loop, break;
}
GeofencingApi was created exactly for this purpose: https://developer.android.com/reference/com/google/android/gms/location/GeofencingApi.html
I think you make mistake createNewMarkerAndCircle(), Please make MarkerOptions and Circle local else when update map only last instance of show, that is reason that only last circle show.
public void createNewMarkerAndCircle() {
//...
MarkerOptions aMarker = mMap.addMarker(new MarkerOptions()
.position(new LatLng(dLat, dLon))
.title(nombre_punto)
.snippet(introduccion_punto)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
Circle aCircle = mMap.addCircle(new CircleOptions()
.center(new LatLng(dLat, dLon))
.radius(150)
.strokeColor(Color.RED)
}
I've a route created along with a polyline over google maps api v2 (Android). My custom marker moves along the route which i achieved by creating a thread. I also get the maneuvers (turn-left, turn-right, etc).
Now as my marker starts moving (a car icon), i want it to turn and navigate through the route. I did manage to rotate it, however i want an accurate solution for this query. right now i hard coded the angles and thus rotating my marker along with the " setRoation() method ". However the rotation isn't as it should be and i cannot get its dynamic angles.
Please Help !
Thanks in advance
Hey see following code for rotate marker as per location
double oldlat = 19.180237, oldlong = 72.855415;
double newlat = 19.180237, newlong = 72.855415;
public void rotatemarker() {
Location prevLoc = new Location("service Provider");
prevLoc.setLatitude(oldlat);
prevLoc.setLongitude(oldlong);
Location newLoc = new Location("service Provider");
newLoc.setLatitude(newlat);
newLoc.setLongitude(newlong);
bearing = prevLoc.bearingTo(newLoc);
map.clear();
map.addMarker(new MarkerOptions()
.position(new LatLng(newlat, newlong))
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher))
.anchor(0.5f, 0.5f)
.rotation(bearing)
.flat(true));
oldlong = newlong;
}
I have a eight different Longitude and latitude values in that any one value have longitude and latitude and remaining wont have values in that case I have to show a radius for that one value. Can anyone help me to solve this problem.
Thanks
GoogleMap has method addCircle so your just need to call it with appropriate params:
GoogleMap map;
MarkerOptions markerOptions = new MarkerOptions();
LatLng position = new LatLng(50, 50);
markerOptions.position(position);
Marker marker = map.addMarker(markerOptions);
Circle circle = map.addCircle(
new CircleOptions()
.center(position)
.radius(100)
.strokeWidth(0f)
.fillColor(getActivity().getResources().getColor(R.color.someColor))
);