I have an app with google maps link like this
const url = Platform.select({
ios: `https://www.google.com/maps/search/?api=1&query=${label}`,
android: `${scheme}${latLng}(${label})`
});
Linking.canOpenURL(url)
What I want to do is to query google maps using query=${lat},${lon} and using the name described as label above, because when I do the query, google maps open at the location but the pin displays the lat and long, and I want to display the label string
Please help!
I just figured it out. If you have google maps installed it will open the app with the desired location and pin correctly. If you don't have google maps installed it will open the browser with google maps and location! :)
const scheme = Platform.select({ ios: 'maps:0,0?q=', android: 'geo:0,0?q=' });
const latLng = `${your Lat},${your Long}`;
const label = this.state.clinic.name;
const url = Platform.select({
ios: `https://www.google.com/maps/search/?api=1&query=${label}¢er=${lat},${long}`,
android: `${scheme}${latLng}(${label})`
});
Linking.canOpenURL(url)
.then((supported) => {
if (!supported) {
browser_url =
"https://www.google.de/maps/#" +
latitude +
"," +
longitude +
"?q=" +
label;
return Linking.openURL(browser_url);
} else {
return Linking.openURL(url);
}
})
.catch((err) => console.log('error', err));
Related
In my Ionic 3 app for Android and iOS, I need to open a specific geolocation with Google Maps (if installed) or Apple Maps. I discovered Launch Navigator which pretty much does the same.
Is there a way by which I can choose not to navigate to the specified location and only show a marker using Launch Navigator?
If not, are there any other alternatives to make this possible?
I had the same issue and this is what I came up with. There are three important steps
Check to see if the user is on iOS or Android
need to include Platform import { Platform } from 'ionic-angular';
If they are on iOS check for Google Maps
I'm using LaunchNavigator to check for the app
import { LaunchNavigator, LaunchNavigatorOptions } from '#ionic-native/launch-navigator';
Open the appropriate app with our GPS parameters
To open an external app we need the In App Browser
import { InAppBrowser } from '#ionic-native/in-app-browser';
then we have all we need to open Google Maps on iOS if it's available, with no navigation
if (this.platform.is('ios')) {
//try google maps first
this.launchNavigator.isAppAvailable(this.launchNavigator.APP.GOOGLE_MAPS).then(
response => {
if(response) {
window.open('comgooglemaps://?q=' + lat + ',' + lng + '(' + marker_name + ')', '_system');
}
else {
window.open('maps://?q=' + lat + ',' + lng, '_system');
}
},
failure => {
//check failed;
}
);
}
else if (this.platform.is('android')) {
window.open('geo://' + lat + ',' + lng + '?q=' + lat + ',' + lng + '(' + marker_name + ')', '_system');
}
Try this, it let the user to choose the app navigation (waze or map or...) to open, and add a marker on latitude and longitude given:
import { Platform } from '#ionic/angular';
...
constructor(
public platform: Platform
) {
}
public openMapsApp(lat: number, lng: number) {
const geocoords = lat + ',' + lng;
if (
this.platform.is('ios')
&& this.platform.is('iphone')
&& this.platform.is('ipad')
) {
window.open('maps://?q=' + geocoords, '_system');
return;
}
if (this.platform.is('desktop')) {
window.open('https://www.google.com/maps?q=' + geocoords);
return;
}
const label = encodeURI('7 East Street'); // encode the label!
window.open('geo:' + geocoords + '?q=' + geocoords + '(' + label + ')', '_system');
}
I'm trying to get the longitude and latitude of the user but I'm seeing that in Codepen the navigator.geolocation.getCurrentPosition is not working.
$(document).ready(function() {
if(navigator.geolocation){
function success(position){
var lat = position.coords.latitude;
var long = position.coords.longitude;
$("p").html("latitude: " + lat + "<br>longitude: " + long);
};
function failure(){
$("p").html("Not working");
};
navigator.geolocation.getCurrentPosition(success,failure);
};
});
Output:
Not Working
So, is there a way around it or do I have to use another API for this?
This is a repeat of javascript - geolocation not working in codepen.
Chrome excerpt:
getCurrentPosition() and watchPosition() are deprecated on insecure origins. To use this feature, you should consider switching your application to a secure origin, such as HTTPS.
I am facing issue using Geolocation pluggin with Ionic and Cordova framework.
Using Visual Studio Community 2015, with Cordova CLI version :4.3.0 and added the org.apache.cordova.geolocation pluggin to VSC-2015
My controller.js
.controller('MapCtrl', function ($scope) {
function onSuccess(position) {
console.log(position.timestamp);
console.log(position.coords.latitude + " " + position.coords.longitude);
}
function onError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
navigator.geolocation.getCurrentPosition(onSuccess, onError);
})
Added the google maps into the index.html
<script src="https://maps.googleapis.com/maps/api/js?sensor=true&v=3.exp"></script>
Added a Map to my Map.html file
<div id="map" data-tap-disabled="true" map> </div>
The problem is I always get a fixed value of Lat/Long
ie Lat = 43.465187 and Long = -80.522372
This is not my correct Geolocation in terms of Lat/Long.I need my current Geolocation in terms of Lat/Long
Please help me identity the fault.
Also I am using Ripple -Nexus (Galaxy) on Browser.
Any help is appreciated...
Thanks,
Download the Latest version of GeoLocation pluggin from https://github.com/apache/cordova-plugin-geolocation
Then inject the $cordovaGeolocation into the controller.
Code is as below:
.controller('HomeCtrl', function($scope,$cordovaGeolocation) {
$scope.getLocation = function() {
var posOptions = {timeout: 10000, enableHighAccuracy: false};
$cordovaGeolocation
.getCurrentPosition(posOptions)
.then(function (position) {
//these are your your lat long values
var lat = position.coords.latitude
var long = position.coords.longitude
}, function(err) {
// error
});
}
})
Note: The device need to be connected to internet or wifi and also need to keep the GPS location ON in the device for getting the correct Lat/Long Values.
I am not sure why you get fixed lat/long values. However to get the device's position using Ionic, the following works for me:
ionic.Platform.ready($scope.getLocation);
$scope.getLocation = function () {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition($scope.showPosition, $scope.showError);
}
else {
console.log('Geolocation is not supported by this browser.');
}
}
$scope.showPosition = function (position) {
console.log(position.timestamp);
console.log(position.coords.latitude + ' ' + position.coords.longitude);
}
$scope.showError = function (error) {
switch (error.code) {
case error.PERMISSION_DENIED:
console.log('User denied the request for Geolocation.');
break;
case error.POSITION_UNAVAILABLE:
console.log('Location information is unavailable.');
break;
case error.TIMEOUT:
console.log('The request to get user location timed out.');
break;
case error.UNKNOWN_ERROR:
console.log('An unknown error occurred.');
break;
}
}
Note that the call to:
ionic.Platform.ready($scope.getLocation);
Is crucial, as you can only guarantee you'll get an accurate GPS fix once the device is ready/initialised.
The Setup
Currently building my phone gap iOS app which includes geolocation in addition to sending the results to a database and getting back nearby locations. Because I am using jQuery Mobile and Phonegap, I was testing on a browser to perfection, geolocation was success and everything returned was as well. On the iOS device, not so much. When sending a set latitude and longitude, everything returned successfully, but when using the google maps geolocation, and eventually the cordova plugin for geolocation, both failed as unable to find my location, even though they are the exact same code, minus the phone gap initializer on the website.
The Code
jQuery
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
$(document).ready(function () {
//Load Google Map & Geolocate
$(document).on("pagecreate", "#game", function () {
// keep as separate function call to allow for DOM to update after a tag
loadGoogleMap();
});
function loadGoogleMap() {
var defaultLatLng = new google.maps.LatLng(34.0983425, -118.3267434); //default location is Hollywood, CA
if (navigator.geolocation) {
function success(pos) {
//set fields for current user latitude and longitude
$("#lat_in").val(pos.coords.latitude);
$("#lon_in").val(pos.coords.longitude);
//location found
drawMap(new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude));
}
function fail(error) {
//try cordova geolocation plugin
function onSuccess(position) {
alert("CDV position success");
//alert(position.coords.latitude);
//alert(position.coords.longitude);
}
function onError(error) {
alert("CDV position error");
//alert("code: " + error.code + "\n" + "message: " + error.message);
}
navigator.geolocation.getCurrentPosition(onSuccess, onError);
alert("CDV position fail. Google geolocate fail"); //WHERE THE ERROR IS THROWN. THIS IS ALWAYS THE OUTCOME
//drawMap(defaultLatLng); //Failed to find location. Show default location
}
//Geolocate. Cache location for 5 mins. Timeout after 6 seconds
navigator.geolocation.getCurrentPosition(success, fail, {
maximumAge: 500000,
enableHighAccuracy: true,
timeout: 6000
});
} else {
drawMap(defaultLatLng); //No geolocation support, show default map
}
function drawMap(latlng) {
//Collect Data to Send
var uname = $("#unm_in").val();
var lat = $("#lat_in").val();
var lon = $("#lon_in").val();
var myOptions = {
zoom: 16,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);
//update latitude and longitude on DB for logged in user && get nearby users
$.post("PHP_ON_SERVER", {
uname: uname,
lat: lat,
lon: lon
}, function (data) {
$.each(data, function (key, value) {
//Define variables
var allUsrLoc = new google.maps.LatLng(value.lat, value.lon);
var contentString = value.uname;
var mkrImg = "inUsrIcon.png"; //icon for logged in user
var inUserName = value.fname + " " + value.lname;
//if info belongs to current user
if (contentString === uname) {
var marker = new google.maps.Marker({
position: allUsrLoc,
map: map,
title: "Player",
icon: mkrImg
});
//change header to name and points
$("#inUserInfo").text("Hi " + inUserName + " || You have " + value.points + " points");
//disable tag button if not it
if (value.isit === "notit") {
$("#tagBtn").addClass("ui-state-disabled");
}
}
//if not current user and is not it
else if (contentString != uname && value.isit != "it") {
var marker = new google.maps.Marker({
position: allUsrLoc,
map: map,
title: "Player"
});
google.maps.event.addListener(marker, 'click', function () {
//Change selected player hidden field to match marker
$("#unm_sel").val(contentString);
$("#lat_sel").val(value.lat);
$("#lon_sel").val(value.lon);
$("#isit_sel").val(value.isit);
//Change tag button to selected player's username
$("#tagBtn").text("Tag " + contentString);
});
}
//no condition for if not current user and is it. TBD if anything will come
});
}, "json");
} //end drawMap
} // end loadGoogleMap
}
HTML
<div data-role="content" role="main" id="map-canvas">
<!-- Map loads here -->
</div>
Some More to Know
I have been reading up on other related questions, but none of the others do work. Either they say use the plugin, which I am to no avail, or set <access origin="*" /> which I am doing already. In addition, most of the other questions revolve around pre-3.0.0 versions of phone gap, which was its own monster. Also, the map loads, just no markers display. On the DB, the user who's location is being updated is set to (0,0). Lastly, when using geolocation in any other app, the iOS device I test on is successfully finding my location. In addition, I am testing on the same WiFi network as the browser which worked, so anything relating to internally being unable to locate due to network or hardware is ruled out.
If you want to see the working example, just ask.
I have used following code to call Google Maps which is installed in Blackberry device
public void invokeGMaps(GMLocation l) {
URLEncodedPostData uepd = new URLEncodedPostData(null, false);
uepd.append("action", "LOCN");
uepd.append("a",
"#latlon:" + l.getLatitude() + "," + l.getLongitude());
uepd.append("title", l.getName());
uepd.append("description", l.getDescription());
String[] args = { "http://gmm/x?" + uepd.toString() };
ApplicationDescriptor ad = CodeModuleManager.getApplicationDescriptors(mh)[0];
ApplicationDescriptor ad2 = new ApplicationDescriptor(ad, args);
try {
ApplicationManager.getApplicationManager().runApplication(ad2, true);
} catch (ApplicationManagerException e) {
System.out.println(e.getMessage());
Dialog.alert("error1 " + e.getMessage());
} catch (Exception e) {
Dialog.alert("error2 " + e.getMessage());
}
}
this is working fine. I have passed current postion to it.
Suppose user will search any particular location on google maps after going on google maps. so while returning back to application i want searched location latitude & longitude from google maps.
So please give me idea how i will get it in my application from google maps application?
Please help me, or any help regarding search location on blackberry how to done it?
Thanks for reading my question patiently.