Asked by:
How to center MapPolyline and add pins at the two points of the polyline ?

Question
-
Hello,
I'm using bing Map in my android app
I'm using the code below to draw a MapPolyline:
mMapView = new MapView(this, MapRenderMode.VECTOR); mMapView.setCredentialsKey(BuildConfig.CREDENTIALS_KEY); ((FrameLayout)findViewById(R.id.map_view)).addView(mMapView); mMapView.onCreate(savedInstanceState);
Geoposition center = mMapView.getCenter().getPosition(); ArrayList<Geoposition> geopoints = new ArrayList<Geoposition>(); // geopoints.add(new Geoposition(47.609466, -122.265185)); // geopoints.add(new Geoposition(-29.0023, 25.0803)); geopoints.add(new Geoposition(47.609466, -122.265185)); geopoints.add(new Geoposition(-29.0023, 25.0803)); MapPolyline mapPolyline = new MapPolyline(); mapPolyline.setPath(new Geopath(geopoints)); mapPolyline.setStrokeColor(Color.BLACK); mapPolyline.setStrokeWidth(3); mapPolyline.setStrokeDashed(true); // Add Polyline to a layer on the map control. MapElementLayer linesLayer = new MapElementLayer(); linesLayer.setZIndex(1.0f); linesLayer.getElements().add(mapPolyline); mMapView.getLayers().add(linesLayer);
The problem is that the MapPolyline is to big between the Usa and South Africa and. The map is not center.
I want the map to be centered, in this case user do not need to scroll to see the two points of the MapPolyline.
My questions are:
How could make the map to be centered and How could I put two icons at the two points of the MapPolyline like below ?:
Actual MapPolyline is:
Thanks.
- Edited by kamkom96 Saturday, July 11, 2020 3:51 PM
Saturday, July 11, 2020 3:21 PM
All replies
-
Hi Kamkom,
I see the "-" is greyed. Are you able to move this map at all? Did you start with one of the sample projects at https://github.com/microsoft/MapsSDK-Native/tree/master/Android
Sincerely,
IoTGirl
Sunday, July 12, 2020 2:58 AM -
Yes I'm able to scroll this map.
Here is Where I started to use that api in my android app:
https://docs.microsoft.com/en-us/bingmaps/sdk-native/getting-started-android/
Have you carefully read my question ?
What did not you undestand of my question ?
My questions are:
How could I make the two points of the MapPolyline visible on screen without having to scroll the map ?
How could I give pins to the two points of the MapPolyline ?
How to center the two points of the MapPolyline below ?
Thanks.
- Edited by kamkom96 Sunday, July 12, 2020 1:54 PM
Sunday, July 12, 2020 1:44 PM -
Hi Kamkom,
Yes, I read your question and was looking at the map limits and am surprised it does not resize automatically based on your endpoints. This is why I asked about the "-" as I would think, based on the contents, that this map could zoom further. My guess is that your segment is too long for the map to show in portrait mode but that is just a guess at this point. I have reached out to the team to take a look at your screen shots and see what might be happening.
If you do make a shorter arc, like Seattle to Miami, does the whole segment show?
Sincerely,
IoTGirl
Monday, July 13, 2020 4:58 PM -
That image below is not a screenshot from and android app.
I'm using that api in my web project.
I did that screenshot from my computer browser to show the sample I want to reproduce in my android project. It is not a horizontal orientation screenshot of an android phone.
Thanks for you help.
- Edited by kamkom96 Monday, July 13, 2020 9:09 PM
Monday, July 13, 2020 9:06 PM -
For the Android map control, setting the view is easy. You want to call MapScene.createFromLocations to get a view including your endpoints
https://docs.microsoft.com/en-us/bingmaps/sdk-native/map-control-api/mapscene-class
To add pins at the endpoints, just add a pair of MapIcons
https://docs.microsoft.com/en-us/bingmaps/sdk-native/map-control-api/mapicon-class
- Proposed as answer by IoTGirlMicrosoft employee Thursday, July 16, 2020 10:29 PM
Tuesday, July 14, 2020 6:26 PM -
MapScene for one location is used like below:
mMapView.setScene( MapScene.createFromLocationAndZoomLevel(LAKE_WASHINGTON_Localisation, 10), MapAnimationKind.NONE);
How about for multiples locations like a polyline ?
Trying that:
Geopoint location_1 = new Geopoint(47.609466, -122.265185); Geopoint location_2 = new Geopoint(-29.0023, 25.0803); mMapView.setScene( MapScene.createFromLocations(location_1, location_2), MapAnimationKind.NONE);
MapScene.createFromLocations(location_1, location_2), is red marked with error message: cannot resolve method createFromLocations(com.microsoft.maps.GoePoint, com.microsoft.maps.GoePoint)
Any solutions ?
- Edited by kamkom96 Monday, July 20, 2020 12:19 PM
Monday, July 20, 2020 12:02 PM -
createFromLocations takes an iterable - try passing it a collection of Geooint, for example a List<Geopoint>
Monday, July 20, 2020 4:17 PM -
Here my full code:
ArrayList<Geoposition> geopoints = new ArrayList<>();
geopoints.add(new Geoposition( 47.609466, -122.265185));
geopoints.add(new Geoposition(-29.0023, 25.0803));
MapPolyline mapPolyline = new MapPolyline();
mapPolyline.setPath(new Geopath(geopoints));
mapPolyline.setStrokeColor(Color.BLACK);
mapPolyline.setStrokeWidth(3);
mapPolyline.setStrokeDashed(true);
ArrayList<Geopoint> geopoint = new ArrayList<>();
geopoint.add(new Geopoint( 47.609466, -122.265185));
geopoint.add(new Geopoint(-29.0023, 25.0803));
// Add Polyline to a layer on the map control.
MapElementLayer linesLayer = new MapElementLayer();
linesLayer.setZIndex(1.0f);
linesLayer.getElements().add(mapPolyline);
mMapView.getLayers().add(linesLayer);
// set Map scene
mMapView.setScene(
MapScene.createFromLocations(geopoint),
MapAnimationKind.NONE);
Using MapScene like below:
ArrayList<Geopoint> geopoint = new ArrayList<>(); geopoint.add(new Geopoint( 47.609466, -122.265185)); geopoint.add(new Geopoint(-29.0023, 25.0803)); mMapView.setScene( MapScene.createFromLocations(geopoint), MapAnimationKind.NONE);
But nothing change, same result. The polyline is not getting centered.
It's sad for bing map.
Thanks.
- Edited by kamkom96 Monday, July 20, 2020 5:02 PM
Monday, July 20, 2020 4:49 PM -
In this case you're choosing points so far apart you're hitting the limit on the minimum zoom level allowed for the control. Try picking points that are closer together.Monday, July 20, 2020 11:25 PM
-
Hi,
I just want to clarify that the limitation on how far to zoom out on the native map control is currently by design. The idea behind this was to ensure that the map is still able to fill the entire screen, especially for the smaller screens on mobile.
Having said that, we are investigating a solution which takes into account your scenario as well as others. Separately, there also appears to be a related bug where the bound calculations are off which needs to be fixed. We'll keep you updated.
Thanks for your feedback.
- Edited by JP_CandelasMicrosoft employee Monday, July 20, 2020 11:41 PM
- Proposed as answer by JP_CandelasMicrosoft employee Monday, July 20, 2020 11:41 PM
Monday, July 20, 2020 11:29 PM -
HEllo,
Any Update Here ?
I'm still waiting.
Tuesday, August 11, 2020 8:17 AM