在向Android发送puish通知后,在Google地图上显示android设备的当前位置 [英] Show the current location of android device on Google map after send puish notification to android
问题描述
问题是当提交按钮单击两个方法时执行...第一个由控制器发送通知给android。第二个是onclcik javascript函数,其中由ajax我们调用的方法显示我们的纬度和经度,并在谷歌地图上显示它们。
现在它执行的第一个javascript函数然后发送推送通知。然后重新加载整页。所以当javascript函数执行后,地图会随着页面的加载而消失。
我尝试了什么:
<控制器中的
:
公共ActionResult getcurrentlocation()
{
返回查看();
}
[HttpPost]
public ActionResult getcurrentlocation(FormCollection fc)
{
string eid = fc [EmployeeCode];
string empid = geteid(eid);
SendNotification(empid,获取当前位置);
返回查看();
}
public void SendNotification(string deviceId,string message)
{
// API在Google项目中创建的密钥
string GoogleAppID =AIzaSyDBhaDX8sCD9gZOgBA_3vZS4apA1oax_rg;
//在Google项目中创建的项目ID
var SENDER_ID ='';
// Android App创建的注册ID,即DeviceId
string regID = deviceId.Trim();
ServicePointManager.ServerCertificateValidationCallback = new
RemoteCertificateValidationCallback
(
delegate {return true ; }
);
WebRequest webRequest;
webRequest = WebRequest.Create(https://android.googleapis.com/gcm/send );
webRequest.Method =post;
webRequest.ContentType =application / x-www-form-urlencoded; charset = UTF-8;
webRequest.Headers.Add(string.Format(Authorization:key = {0},GoogleAppID));
webRequest.Headers.Add(string.Format(发件人:id = {0},SENDER_ID));
string postData = string.Format(collapse_key = score_update& time_to_live = 108& delay_while_idle = 0& data.message = {0}& data.time = {1}& registration_id = {2},message,DateTime.UtcNow,regID,default);
Byte [] byteArray = Encoding.UTF8.GetBytes(postData) ;
webRequest.ContentLength = byteArray.Length;
流dataStream = webRequest.GetRequestStream();
dataStream.Write(by teArray,0,byteArray.Length);
dataStream.Close();
WebResponse webResponse = webRequest.GetResponse();
dataStream = webResponse.GetResponseStream();
使用(StreamReader streamReader = new StreamReader(dataStream))
{
String sResponseFromServer = streamReader.ReadToEnd();
streamReader.Close();
dataStream.Close();
webResponse.Close();
}
}
在视图中:
the prob is when submit button click two method executed...first one by controller that send the notification to android. second one by onclcik javascript function in which by ajax we are calling method that shows us latitude and longitude and showing them on google map.
now its executed first javascript function then send push notification. then reload the full page. so map came and gone as page loaded after javascript function executed.
What I have tried:
in controler:
public ActionResult getcurrentlocation()
{
return View();
}
[HttpPost]
public ActionResult getcurrentlocation(FormCollection fc)
{
string eid = fc["EmployeeCode"];
string empid = geteid(eid);
SendNotification(empid, "Get Current Location");
return View();
}
public void SendNotification(string deviceId, string message)
{
//API Key created in Google project
string GoogleAppID = "AIzaSyDBhaDX8sCD9gZOgBA_3vZS4apA1oax_rg";
//Project ID created in Google project
var SENDER_ID = '';
//Registration Id created by Android App i.e. DeviceId
string regID = deviceId.Trim();
ServicePointManager.ServerCertificateValidationCallback = new
RemoteCertificateValidationCallback
(
delegate { return true; }
);
WebRequest webRequest;
webRequest = WebRequest.Create("https://android.googleapis.com/gcm/send");
webRequest.Method = "post";
webRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
webRequest.Headers.Add(string.Format("Authorization: key={0}", GoogleAppID));
webRequest.Headers.Add(string.Format("Sender: id={0}", SENDER_ID));
string postData = string.Format("collapse_key=score_update&time_to_live=108&delay_while_idle=0&data.message={0} &data.time={1} ®istration_id={2}", message, DateTime.UtcNow, regID, "default");
Byte[] byteArray = Encoding.UTF8.GetBytes(postData);
webRequest.ContentLength = byteArray.Length;
Stream dataStream = webRequest.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse webResponse = webRequest.GetResponse();
dataStream = webResponse.GetResponseStream();
using (StreamReader streamReader = new StreamReader(dataStream))
{
String sResponseFromServer = streamReader.ReadToEnd();
streamReader.Close();
dataStream.Close();
webResponse.Close();
}
}
in view:
<style>#map_canvas { height: 100% }</style>
<script language="javascript" type="text/javascript">
</script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="jquery-1.9.1.min.js" type="text/javascript"></script>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="../Scripting/UIBlocker.js"></script>
<script src="../Scripting/spin.js"></script>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=AIzaSyDY0kkJiTPVd2U7aTOAwhc9ySH6oHxOIYM&sensor=false"></script>
<header class="head">
<h3>Employee Location</h3>
</header>
<div class="row" id="rr">
<div class="col-lg-6">
@using (Html.BeginForm("getcurrentlocation", "Setup", FormMethod.Post, new { @class = "form-horizontal"}))
{
@Html.HiddenFor(m=>m.Id)
<div class="form-group">
@Html.Label("Employee Code", new { @class = "control-label col-lg-4", })
<div class="col-lg-8">
@Html.TextBoxFor(m => m.EmployeeCode, new { @class = "form-control",@name = "eid",@id = "emp" , @placeholder = "Enter Employee Code." })
</div>
@Html.ValidationMessageFor(m => m.EmployeeCode)
</div>
<div class="col-lg-8">
<input type="submit" id="Add" value="send request" name="Command" class="btn btn-metis-6 btn-sm" />
</div>
<br />
} </div></div>
<div class="col-lg-8">
<input type="submit" id="kk" value="Get current location" name="Command" onclick="GetEngineerLocation()" class="btn btn-metis-6 btn-sm" />
</div>
<div id="map_canvas" style="width: 500px; height: 400px;"></div>
<div id="foo">
</div>
<div class="col-lg-12">
</div>
function GetEngineerLocation() {
//var empId = document.getElementById('ddlEmployeelist').value;
var callId = document.getElementById('emp').value;
if (callId == undefined || callId == '') {
callId = -1;
}
if (callId != '') {
$.ajax({
type: "Post",
url: "/MobileApp/Setup/currentlocation",
dataType: 'json',
data: JSON.stringify({ callId: callId }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: GetEngineerCurrentLocationSuccess,
error: GetEngineerCurrentLocationFail
});
}
else {
alert("Please select employeee")
}
}
function GetEngineerCurrentLocationSuccess(result) {
var lat = result.Latitude;
var lon = result.Longitude;
var time = result.LocationTime;
var myLatlng = new google.maps.LatLng(lat, lon) // This is used to center the map to show our markers
var mapOptions = {
center: myLatlng,
zoom: 6,
mapTypeId: google.maps.MapTypeId.ROADMAP,
marker: true
};
var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
var marker = new google.maps.Marker({
position: myLatlng,
title: "lat: " + lat + " long: " + lon + " Time: " + time
});
marker.setMap(map);
var infowindow = new google.maps.InfoWindow({ content: "<b>User Address</b><br/> Latitude:" + lat + "<br /> Longitude:" + lon + "" });
infowindow.open(map, marker);
}
function GetEngineerCurrentLocationFail() {
alert("fail");
}
推荐答案
.ajax({
type: 发布,
url: / MobileApp / Setup / currentlocation,
dataType:' json',
data: JSON .stringify({callId:callId}),
contentType: application / json; charset = utf-8,
dataType: json,
成功:GetEngineerCurrentLocationSuccess,
错误:GetEngineerCurrentLocationFail
}) ;
}
else {
alert( 请选择employeesee)
}
}
function GetEngineerCurrentLocationSuccess(结果){
var lat = result.Latitude;
var lon = result.Longitude;
var time = result.LocationTime;
var myLatlng = new google.maps.LatLng(lat,lon) // 这用于将地图居中以显示我们的标记
var mapOptions = {
center:myLatlng,
zoom: 6 ,
mapTypeId:google .maps.MapTypeId.ROADMAP,
marker: true
};
var map = new google.maps。 Map ( document .getElementById( map_canvas ),mapOptions);
var marker = new google.maps.Marker({
position:myLatlng ,
title: lat: + lat + long: + lon + 时间: +时间
});
marker.setMap(map);
var infowindow = new google.maps.InfoWindow({content: < b>用户地址< / b>< br />纬度: + lat + < br />经度: + lon + });
infowindow.open(地图,标记);
}
function GetEngineerCurrentLocationFail(){
alert( fail);
}
.ajax({ type: "Post", url: "/MobileApp/Setup/currentlocation", dataType: 'json', data: JSON.stringify({ callId: callId }), contentType: "application/json; charset=utf-8", dataType: "json", success: GetEngineerCurrentLocationSuccess, error: GetEngineerCurrentLocationFail }); } else { alert("Please select employeee") } } function GetEngineerCurrentLocationSuccess(result) { var lat = result.Latitude; var lon = result.Longitude; var time = result.LocationTime; var myLatlng = new google.maps.LatLng(lat, lon) // This is used to center the map to show our markers var mapOptions = { center: myLatlng, zoom: 6, mapTypeId: google.maps.MapTypeId.ROADMAP, marker: true }; var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions); var marker = new google.maps.Marker({ position: myLatlng, title: "lat: " + lat + " long: " + lon + " Time: " + time }); marker.setMap(map); var infowindow = new google.maps.InfoWindow({ content: "<b>User Address</b><br/> Latitude:" + lat + "<br /> Longitude:" + lon + "" }); infowindow.open(map, marker); } function GetEngineerCurrentLocationFail() { alert("fail"); }
当您点击获取当前位置按钮时,您忘记取消表单提交:
You forgot to cancel the form submission when you click the "get current location" button:
<input type="submit" id="kk" value="Get current location" name="Command" onclick="GetEngineerLocation();return false;" class="btn btn-metis-6 btn-sm" />
在调用 GetEngineerLocation(); $ c后添加
返回false;
$ c>阻止提交表单。
Adding return false;
after the call to GetEngineerLocation();
prevents the form from being submitted.
这篇关于在向Android发送puish通知后,在Google地图上显示android设备的当前位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!