Since the Google I/O 2013 we can read in several places of the documentation of Google Maps Android API v2 that
This method / interface is deprecated.
use com.google.android.gms.location.LocationClient instead. LocationClient provides improved location finding and power usage and is used by the "My Location" blue dot. See the MyLocationDemoActivity in the sample applications folder for example example code, or the Location Developer Guide.
While it is great the new API appeared, I can't think of any good reason to put #Deprecated on these methods.
They are indeed a very convenient way of accessing location pointed to by the blue dot.
A single line
Location location = map.getMyLocation();
to retrieve my location vs the amount of setup code we need to write using the new Location API to achieve the same result speaks against having them deprecated.
From the MyLocationDemoActivity (under ANDROID_SDK/extras/google/google_play_services/samples/maps/src/com/example/mapdemo/MyLocationDemoActivity.java):
// These settings are the same as the settings for the map. They will in fact give you updates at
// the maximal rates currently possible.
private static final LocationRequest REQUEST = LocationRequest.create()
.setInterval(5000) // 5 seconds
.setFastestInterval(16) // 16ms = 60fps
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
The comment suggests that these hardcoded values are the same as in the map, but this may change in an undesirable way in future.
So after all the pros and cons are here, what could be the reason behind desision to deprecate these APIs?.
One more point. If GoogleMap uses LocationClient, but we don't have access to its results and we have to use another LocationClient and bunch of listeners, it is two LocationClients for the same task. Pure waste of resources.
Related
I am trying to use Google Maps for Android, offline (always and forever).
Surprisingly, I can't find any question here that asks or solves this issue specifically.
When I use a new offline phone, both my app and Google Maps show a blank map (dah, no map loaded) and 'my' location blue dot is not shown. Well, actually, no marker is shown.
To Reproduce
Restore any Android phone to its factory settings
Enable location services (GPS, without connecting to the internet at any stage)
Open the Google Maps app
--> See that there is no 'my location' blue marker, although when you long click on the screen, the app shows its coordinates (meaning, GPS does work, but the map doesn't show it)
Technical Symptoms
Even when I load offline maps (.mbtiles format, custom ones, not Google's) they're still not shown (nor the markers). It's like Google put some code like this:
if (no internet) hideAllViews().
Note that once I connect the phone to the internet, our custom tiles do work, even if I later turn the phone offline.
I can interact with the map (long click to view the clicked location, for example, which shows that my GPS location indeed works), but that's about it (until I connect the phone to the internet, from which point I can turn it offline again but with everything surprisingly working).
Code Example - a simplified version
//build.gradle:
implementation 'com.google.android.gms:play-services-maps:17.0.0'
//MapActivity.kt
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.MapView
import com.google.android.gms.maps.MapsInitializer
class MapActivity : AppCompatActivity {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.map_activity)
val mapFragment = supportFragmentManager.findFragmentByTag(MAP_FRAGMENT_TAG) as MapFragment?
?: MapFragment().also {
supportFragmentManager.beginTransaction().add(R.id.map, it, MAP_FRAGMENT_TAG).commit()
}
mapFragment.getMapAsync(::onMapReady)
mapFragment.retainInstance = true
}
private fun onMapReady(map: GoogleMap) {
map.isMyLocationEnabled = true
map.uiSettings.isMyLocationButtonEnabled = true
Log.d("GoogleMap", "Map should be ready and visible with my-location marker shown, if phone's GPS is enabled")
}
}
I hope someone here knows a trick or worked at Google and can shed some light on this.
Thank you!
As opposed to most similar questions, I do know how to make this work (internet...) but am asking specifically about a use case where a new phone can never be connected to the internet - not even one time for one second.
I am familiar with other offline maps services, but am trying to solve this with Google's maps, at least for now
It is not possible to load the API without connecting to the internet first since it was designed to be used online(As of now), so this is Working As Intended.
Please note that using Maps SDK for android requires an internet connection first to load because it checks the API key. Then you can use the Map offline for a certain period of time(there's no definite period of time for offline functionality that requires you to be online again)
But there are customers who are also interested in this functionality, so there is an ongoing entry for it in the Google Issue Tracker that was created since 2013 to let the API users be aware of this feature request.
You can view and star the feature request here:
https://issuetracker.google.com/35823181
Please note that the Issue Tracker entry above is the authoritative source for public information regarding the aforementioned feature requests, and all publicly-relevant updates will be posted there.
We have the next problem:
We are consulting the gps location in order to get a list that contains only those that are close in a radius, but when consulting the GPS it gives us timeout after at leat 40 seconds of waiting.
The problem especially occurs in Android with a Xiaomi Redmi 8 in industrial environments, with less connection than usual or on the move, they don´t have access to LTE all the time.
We use the library # react-native-community / geolocation or navigation.position
Can you help us with any other library or strategy to obtain GPS position that does not consists in the gps wacthing changes on its position all the time? something like getting it on the background..
Code
Geolocation.getCurrentPosition(
position => {
// process the position
},
error => {
console.log(error);
}, {
timeout: 40000,
maximumAge: 0
}
);
As cited from library's github page:
Currently, on Android, this uses the android.location API. This API is not recommended by Google because it is less accurate and slower than the recommended Google Location Services API. This is something that we want to change in the near future https://github.com/react-native-community/react-native-geolocation/issues/6.
There also are some library suggestions in the readme, that actually use Google Location Services API. I've used react-native-location, but this package isn't actively maintained anymore, so I switched to react-native-geolocation-service, which has a very similar API to what you're already using, more specifically the getCurrentPosition.
Perhaps you should give it a try.
react-native-community/geolocation does not utilize the more accurate Google Location Services API, as stated in readme
The recommended library for GPS location (and the one I used) for RN Android is react-native-geolocation-service. Its Readme also mentions a timeout issue which makes me wonder if it is the same as yours:
This library is created in an attempt to fix the location timeout
issue on android with the react-native's current implementation of
Geolocation API. This library tries to solve the issue by using Google
Play Service's new FusedLocationProviderClient API, which Google
strongly recommends over android's default framework location API. It
automatically decides which provider to use based on your request
configuration and also prompts you to change the location mode if it
doesn't satisfy your current request configuration.
So if I were you I would give it a go. The API is meant to be a drop-in replacement for the library you used so migrating should be just a matter of changing packages.
What I have: native android app that use Android Maps API v2 (native library)
Everything works good in Hong Kong and Ukraine. The problems came as soon as we step in China border. We have next problems:
Map is loading from 30 mins to about one hour.
Even when it's loaded, the map resolution is very low http://i.gyazo.com/d4f40e1a225bd48a90087c39a502e4a6.png
Google Map WORKS by itself: http://i.gyazo.com/dc85e3618c285ced5bba799053dd0306.png
What I know around China:
Google is not really friendly there, but our app is not the one that is used a google maps in China, so my problem probably is not a new one.
There is the option to use a google maps hosted locally in China - ditu.google.cn, but as soon as we are using the native app there is not an option now to use a web view of map instead of native implementation (we have a lot of customizations)
I know, that better do not use HTTPS with Google maps (you can read around this here: Google Maps Geolocation API for China)
The question is:
Could we somehow improve the load of map? Could we somehow override the google map source to use a ditu.google.cn?
Could we somehow disable HTTPS in Map API?
Any other solution around this problem, that can be applied based on google services. Maybe there is some undocumented features?
What is did not expect as an answer is something like "Use Baidu". I know Baidu and this is a veeerryyy backup option as soon as they have an api docs in chineese.
It's fare to add, that Geocoding API based on ditu.google.cn works good.
Looking forward, thank you!
Maybe you can't override the original tile source. But you can use TileOverlay to get maps tile from external server.
Usage example :
TileProvider tileProvider = new UrlTileProvider(256, 256) {
//...
#Override
public URL getTileUrl(int x, int y, int zoom) {
/* Define the URL pattern for the tile images */
String s = String.format("http://my.image.server/images/%d/%d/%d.png",
zoom, x, y);
if (!checkTileExists(x, y, zoom)) {
return null;
}
try {
return new URL(s);
} catch (MalformedURLException e) {
throw new AssertionError(e);
}
}
//...
}
You can replace the my.image.server with ditu.google.cn and use external server API to get tile images. In the Maps, you can set OnCameraChangeListener, so the application will load tiles from external server when the map is zooming or panning.
Read more : https://developers.google.com/maps/documentation/android/tileoverlay
Google still has no map publishing license in China (at least as my latest info goes) It may well be that in the near future Google's Map services will be blocked.
A lot of Chinese websites, including ours, use Google's JS as well as their Service API's, Among them is Google's geocoding api. in china, baidu (NASDAQ:BIDU) and sogou [the minor search engine, part of sohu (NASDAQ:SOHU)] both provided online maps and also APIs,
baidu’s map api home is http://dev.baidu.com/wiki/map/in...
sogou’s map api home is http://map.sogou.com/api/
actually, in china, as of now, the google maps api are still available now. as from recent chinatech.us news, Google China passed the annual inspection and got more opportunity to get online map business license so, the 3 API are are and available.
I'm currently working on an application that takes advantage of some simple features of the google maps api v2. This application is supposed to find user's current location and then display some places of my choice which also should be near to the user.
I've already achieved to find and display user location. The thing is that it seems that getMyLocation() (which I'm currently using) is deprecated, acording to android's developer site. However, Eclipse is not showing any kind of warning sign of deprecation for this method. I didn't knew this at the time I started writing my code.
So my question is: should I be worried for this to be a problem in the future if I plan to publish my app?
Here's my code for moving into user location:
if (map.getMyLocation() != null) {
Toast.makeText(this, "You are here", Toast.LENGTH_LONG).show();
map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(
map.getMyLocation().getLatitude(), map.getMyLocation()
.getLongitude()), 15));
}
Thank you very much in advance for your time and your answers.
As said in the documentation, it is deprecated.
Try to use a LocationClient implementation instead.
http://developer.android.com/reference/com/google/android/gms/location/LocationClient.html
You can enable the "my location layer" available on maps V2.
map.setMyLocationEnabled (true)
check:
http://developer.android.com/reference/com/google/android/gms/maps/GoogleMap.html#setMyLocationEnabled%28boolean%29
I want to show local points of interest to the users.
Since this isn't the main goal of my app, I wish to find a rather simple solution,like sending the user to Google Places or any other location based app.
Is there a way of doing that?
If the answer is no, how could I do that? Maybe using some API?
Thanks
You can use the android location and maps api documented here. If not, you can always call the Maps intent but you have to make sure google maps is installed before using it. Here is an example using the Location api from the android dev page linked here:
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
makeUseOfNewLocation(location);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
Now register your application with google API here. Once you get your API key you can use an http request to https://maps.googleapis.com/maps/api/place/search/json?location=&radius=&types=&name=&sensor=&key=YOUR_API_KEY.
You can find the definition of each parameter from the google maps api site here but this is a little list:
location (required) — The latitude/longitude around which to retrieve Place information. This must be provided as a google.maps.LatLng object.
radius (required) — The distance (in meters) within which to return Place results. The recommended best practice is to set radius based on the accuracy of the location signal as given by the location sensor. Note that setting a radius biases results to the indicated area, but may not fully restrict results to the specified area.
types (optional) — Restricts the results to Places matching at least one of the specified types. Types should be separated with a pipe symbol (type1|type2|etc). See the list of supported types.
language (optional) — The language code, indicating in which language the results should be returned, if possible. See the list of supported languages and their codes. Note that we often update supported languages so this list may not be exhaustive.
name (optional) — A term to be matched against the names of Places. Results will be restricted to those containing the passed name value. When a name is included, the area being searched may be broadened, to ensure a suitable number of results.
sensor (required) — Indicates whether or not the Place request came from a device using a location sensor (e.g. a GPS) to determine the location sent in this request. This value must be either true or false.
key (required) — Your application's API key. This key identifies your application for purposes of quota management and so that Places added from your application are made immediately available to your app. Visit the APIs Console to create an API Project and obtain your key.
Here's a quick HTTP request example in android:
HttpGet myGet = new HttpGet("https://maps.googleapis.com/maps/api/place/search/json?location=&radius=&types=&name=&sensor=&key=YOUR_API_KEY");
Once you got your return result, you can parse the response using any json library such as google-gson from here.
Ryan
Your question is not clear but from your question i am able to know that you want to display location of user interest correct me if i m wrong.
if you want to same then
1) you need latitude and longitude of that points
for that you can use any API of you can get those from your webservice
2) after getting list of latitude and longitude you can display those on map by using Itemized Overlay