google maps marker array? - android

Hi guys and girls if any :))
this piece of code creates a null pointer exception
I would like to put a couple of markers in MO
LAT = Double.parseDouble(PartA);
LNG = Double.parseDouble(PartB);
LatLng Standort = new LatLng(LAT, LNG);
MO[y] = mMap.addMarker(new MarkerOptions()
.position(Standort)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.kreis)));
builder.include(MO[y].getPosition());
Array type expected - found com.google.android.gms.maps.model.maker
Definition private static Marker MOrt = null;

I don't know my answer help you out or not.
First of all declare array , than add points and for adding marker i just start a loop with add marker function.
LatLng[] point_new = new LatLng[3];
point_new[0] = new LatLng(24.8926596, 67.0835093);
point_new[1] = new LatLng(48.85837,2.294481);
point_new[2] = new LatLng(0, 0);
for (int i = 0; i < point_new.length; i++) {
drawMarker(point_new[i]);
}
//drawMarker method
private void drawMarker(LatLng point) {
// Creating an instance of MarkerOptions
MarkerOptions markerOptions = new MarkerOptions();
// Setting latitude and longitude for the marker
markerOptions.position(point);
// Adding marker on the Google Map
map.addMarker(markerOptions);
}
final output
hope this will help!
Happy coding

Related

add an ArrayList of markers to a google map

I am trying to display multiple markers on a google map but I seem to only get the last one to display. The reason is that the markers get overwritten in a for loop. I have created an ArrayList that will store the markers but I don't how to add it to the google Map..
the polylines library contains a method called addAll that adds an arraylist of polylines but markers does not have such a thing..could anyone suggest a way to add multiple markers to the map?
public void mapForGPS(GoogleMap googleMap) {
mMap = googleMap;
double[] firstCoords;
double[] lastCoords;
double[] receivedDataArray;
double latitude;
double longitude;
double[] firstSetOfCoordinatesFromArray;
double[] lastSetOfCoordinatesFromArray;
double[] tempCoordinatesArray;
String[] tempDurationAndTimeArray;
String time;
GpsData gpsDataObj;
String[] dataForOneGreenDot;
String[] arrayOfGreenDots = new String[3];
// this will store coordinates for one iteration of a selected day
ArrayList<LatLng> coordList = new ArrayList<LatLng>();
ArrayList<String[]> arrayListOfGreenDots = new ArrayList<String[]>();
// clear polyline array that has coordinates form previous iteration
coordList.clear();
// clears all the markers on the map for the next iteration
mMap.clear();
// loop through JSON array of coordinates and assign data into a coordinates array with an
// object of Latitude and Longitude
for (int i = 0; i <= arrayOfGpsData.size() - 1; i++) {
gpsDataObj = arrayOfGpsData.get(i);
tempCoordinatesArray = gpsDataObj.getArrayOfDoubles();
tempDurationAndTimeArray = gpsDataObj.getArrayOfStrings();
latitude = tempCoordinatesArray[0];
longitude = tempCoordinatesArray[1];
coordList.add(new LatLng(latitude,longitude));
time = tempDurationAndTimeArray[0];
String latString = String.valueOf(latitude);
String longString = String.valueOf(longitude);
arrayOfGreenDots[0] = latString;
arrayOfGreenDots[1] = longString;
arrayOfGreenDots[2] = time;
arrayListOfGreenDots.add(arrayOfGreenDots);
}
// if there was a previous polyline on the map, clear it
if(polylineGPS != null){
polylineGPS.remove();
}
// add coordinates to a polylineGPS object
options = new PolylineOptions().addAll(coordList).width(5).color(Color.BLUE).geodesic(true);
polylineGPS = mMap.addPolyline(options);
// if user selected date in date-time picker then marker will point to
// first set of coordinates in the coordinatesArray and the map will zoom in on that point
if(!arrayOfGpsData.isEmpty())
{
double[] coordinatesFromFirstIndex = new double[2];
String[] timeAndDurationFromFirstIndex = new String[2];
double[] coordinatesFromLastIndex = new double[2];
String[] timeAndDurationFromLastIndex = new String[2];
GpsData objectDataFromFirstIndex = new GpsData(coordinatesFromFirstIndex, timeAndDurationFromFirstIndex);
GpsData objectDataFromLastIndex = new GpsData(coordinatesFromLastIndex, timeAndDurationFromLastIndex);
// get first set of coordinates from array as starting position
objectDataFromFirstIndex = arrayOfGpsData.get(0);
firstSetOfCoordinatesFromArray = objectDataFromFirstIndex.getArrayOfDoubles();
// get last set of coordinates from array as ending position
objectDataFromLastIndex = arrayOfGpsData.get(arrayOfGpsData.size() - 1);
lastSetOfCoordinatesFromArray = objectDataFromLastIndex.getArrayOfDoubles();
//timeAndDurationFromFirstIndex = objectDataFromFirstIndex.getArrayOfStrings();
// place marker for starting position
LatLng firstLocationPointOfDevice = new LatLng(firstSetOfCoordinatesFromArray[0], firstSetOfCoordinatesFromArray[1]);
mMap.addMarker(new MarkerOptions().position(firstLocationPointOfDevice).icon((BitmapDescriptorFactory.fromResource(R.drawable.blue_marker))).title("Starting Location"));
// place marker for ending position
LatLng lastLocationPointOfDevice = new LatLng(lastSetOfCoordinatesFromArray[0], lastSetOfCoordinatesFromArray[1]);
mMap.addMarker(new MarkerOptions().position(lastLocationPointOfDevice).icon(BitmapDescriptorFactory.fromResource(R.drawable.pink_marker)).title("Ending Location"));
// zoom in on first set of coordinates form coordinatesArray
mMap.moveCamera(CameraUpdateFactory.newLatLng(firstLocationPointOfDevice));
// camera zoom in position
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(firstLocationPointOfDevice, 10));
List<Marker> markers = new ArrayList<Marker>();
// adding markers to array list
for(int k = 0; k <= arrayListOfGreenDots.size() - 1; k++){
dataForOneGreenDot = arrayListOfGreenDots.get(k);
double latSingle = Double.parseDouble(dataForOneGreenDot[0]);
double longSingle = Double.parseDouble(dataForOneGreenDot[1]);
String timeSingle = dataForOneGreenDot[2];
//drawMarker(latSingle, longSingle, timeSingle);
LatLng greenDot = new LatLng(latSingle, longSingle);
marker = mMap.addMarker(new MarkerOptions().position(greenDot).icon(BitmapDescriptorFactory.fromResource(R.drawable.green_dot)).title(timeSingle));
markers.add(marker);
}
}
// if user did not select any date from date time picker, then by default the map will zoom on vancouver
else if(arrayOfGpsData.isEmpty())
{
// default location if no date is selected
LatLng defaultLocation = new LatLng(49.2827, -123.1207);
mMap.moveCamera(CameraUpdateFactory.newLatLng(defaultLocation));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(defaultLocation, 10));
}
}
I really don't know why this question got downvoted because it addresses a legit concern for which there was no immidiate answer.
I have found a solution though:
for (int i = 0; i <= arrayOfGpsData.size() - 1; i++) {
gpsDataObj = arrayOfGpsData.get(i);
tempCoordinatesArray = gpsDataObj.getArrayOfDoubles();
tempDurationAndTimeArray = gpsDataObj.getArrayOfStrings();
latitude = tempCoordinatesArray[0];
longitude = tempCoordinatesArray[1];
coordList.add(new LatLng(latitude,longitude));
time = tempDurationAndTimeArray[0];
mMap.addMarker(new MarkerOptions().position(new LatLng(latitude,longitude))
.icon(BitmapDescriptorFactory.fromResource(R.drawable.green_dot_6px))
.title(time)
);
}
as I loop through my arrayOfGpsData and assign lat and long to my LatLng object, I add a marker to that LatLng right away with a time stamp.
I hope this helps someone in the future :D

How to create multiple markers with infoWindow showing google maps api

I'm using google directions and make a route between two points and setting a marker and I need to set a window with some information in both of these markers, but According to google I just can show a window one at the time! But in Uber App they did with both points.
This is what I did :
public void drawRoute(){
PolylineOptions po;
if(polyline == null){
po = new PolylineOptions();
for(int i = 0, tam = latLngs.size(); i < tam; i++){
po.add(latLngs.get(i));
}
po.color(Color.BLACK).width(10);
polyline = mMap.addPolyline(po);
LatLng myCurrentLocation = new LatLng(lat, lon);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(myCurrentLocation, 11));
Marker mMarker;
mMarker = mMap.addMarker(new MarkerOptions().position(finalLocaltion).title(finalLocationName));
mMarker.showInfoWindow();
}
else{
polyline.setPoints(latLngs);
}
}
The window only appears when I click, and not by default!

Google map v2 Heatmap population

Would it be possible to use a heat map to represent/show the amount of people in the specific area using google map v2?
If not , would there be other library or API that I can use to know represent the amount of people in the area.
reference that I saw on the internet: http://googlemapsmania.blogspot.com/2014/02/population-mapping.html
Hey here is the example how you can use heatmap and show your max population
Add all marker in your map then use this code.
HeatmapTileProvider mProvider;
TileOverlay mOverlay;
// Add your all lat lng inside this array list and pass it to addHeatMap
List<LatLng> list = new ArrayList<LatLng>();
double puLat = 0, puLng = 0;
private void createMarker() {
double cLat, cLng;
cLat = locationCur.getLatitude();
cLng = locationCur.getLongitude();
Marker testMarker = googleMap.addMarker(new MarkerOptions()
.position(new LatLng(cLat, cLng)).draggable(true)
.title("Demanding Areas").flat(false));
testMarker.showInfoWindow();
}
private void addHeatMap() {
mProvider = new HeatmapTileProvider.Builder().data(list).build();
mOverlay = googleMap.addTileOverlay(new TileOverlayOptions()
.tileProvider(mProvider));
}
Hope this will help you

Showing multiple markers in google map v2

i have a set of lat long coordinates. i want to display all of those markers at a time on google map. I know how to display a single marker. But don't know how to display multiples at once. Anybody please help me.
Thank you.
i have lat long points.
like
id = 123 lat = 12.00 lon = 77.00
id = 124 lat = 12.01 lon = 77.01
etc.
May This Help You
for(int pin=0; pin<pins.size(); pin++)
{
LatLng pinLocation = new LatLng(Float.parseFloat(pins.get(pin).latitude), Float.parseFloat(pins.get(pin).longitude));
Marker storeMarker = map.addMarker(new MarkerOptions()
.position(pinLocation)
.title(pins.get(pin).pinname)
.snippet(pins.get(pin).address)
);
}
Try this:
LatLng one = new LatLng(2.40744, 77.014702);//Latitude and long points
LatLng two = new LatLng(2.407440, 77.014702);
.......
Similarly u can use more lat and long
myMarkerOne = gm.addMarker(new MarkerOptions()
.position(one)//use LatLng obj
.title("C")
.snippet("dsfd")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
myMarkerTwo = gm.addMarker(new MarkerOptions()
.position(two)
.title("C")
.snippet("dsfds")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
refer this: How to add multiple marker and overlay in v2 google map?
Also check this link:
Create a LatLng object by using following format:
LatLng location = new LatLng(latitude, longitude);
Then call the method with your LatLng object along with your GoogleMap object:
private Marker setCurrentLocation(LatLng location, GoogleMap map) {
BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory
.fromResource(R.drawable.user_location_pin);
Marker marker = map.addMarker(new MarkerOptions().position(location)
.icon(bitmapDescriptor).title(location.toString()));
return marker;
}

How to add markers on source and destination on load of a map using the google map api v2

public void onLoad() {
// Opening the sharedPreferences object
preferences = getSharedPreferences("location", 0);
// Getting number of locations already stored
routeType = preferences.getInt("routeType", 0);
// Getting stored zoom level if exists else return 0
String zoom = preferences.getString("zoom", "0");
// If locations are already saved
if (routeType != 0) {
String lat = "sourceAdd";
String lng = "destinationAdd";
// Iterating through all the locations stored
for (int i = 0; i < routeType; i++) {
// Getting the latitude of the i-th location
lat = preferences.getString("lat" + i, "0");
// Getting the longitude of the i-th location
lng = preferences.getString("lng" + i, "0");
// Drawing marker on the map
drawMarker(new LatLng(Double.parseDouble(lat),
Double.parseDouble(lng)));
}
// Moving CameraPosition to last clicked position
map.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(Double
.parseDouble(lat), Double.parseDouble(lng))));
// Setting the zoom level in the map on last position is clicked
map.animateCamera(CameraUpdateFactory.zoomTo(Float.parseFloat(zoom)));
}
}
public void drawMarker(LatLng point) {
// Creating an instance of MarkerOptions
MarkerOptions markerOptions = new MarkerOptions();
// Setting latitude and longitude for the marker
markerOptions.position(point);
// Adding marker on the Google Map
map.addMarker(new MarkerOptions()
// map.addMarker(markerOptions);
.title("Marker")
.snippet("Marker Yo Yo")
.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
.position(point)
);
}
Hi,
I need to add 2 markers on the map such as the source and the destination, i used the above code and when i run the application i am getting a black line displaying the route between source and destination but the markers are not appearing on the map:( can someone help me?
try this code
public void drawMarker(LatLng source_point,LatLong destination_point) {
// Creating an instance of MarkerOptions
MarkerOptions markerOptions1 = new MarkerOptions();
markerOptions1.title("Marker");
markerOptions1.snippet("Marker Yo Yo");
markerOptions1.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
markerOptions1.position(source_point);
MarkerOptions markerOptions2 = new MarkerOptions();
markerOptions2.title("Marker2");
markerOptions2.snippet("Marker Xo Xo");
markerOptions2.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
markerOptions2.position(destination_point);
// Adding marker on the Google Map
map.addMarker(markerOptions1);
map.addMarker(markerOptions2);
}

Categories

Resources