GVKun编程网logo

Swift中的反向地理编码位置

13

对于想了解Swift中的反向地理编码位置的读者,本文将提供新的信息,并且为您提供关于4.7Swift中swift中的switch语句、Android反向地理编码有问题、angularjs–Ionic:

对于想了解Swift中的反向地理编码位置的读者,本文将提供新的信息,并且为您提供关于4.7 Swift中swift中的switch 语句、Android反向地理编码有问题、angularjs – Ionic:反向地理编码、Angular上的传单Esri地理编码未导入地理编码类的有价值信息。

本文目录一览:

Swift中的反向地理编码位置

Swift中的反向地理编码位置

关闭。 这个问题不能重现,或者是由错别字引起的。它当前不接受答案。


想改善这个问题吗? 更新问题,使其成为Stack Overflow 的主题。

5年前关闭。

改善这个问题

我输入的是纬度和经度。我需要使用reverseGeocodeLocationswift 的功能,以便向我提供位置信息的输出。我尝试使用的代码是

            println(geopoint.longitude)             println(geopoint.latitude)            var manager : CLLocationManager!            var longitude :CLLocationDegrees = geopoint.longitude            var latitude :CLLocationDegrees = geopoint.latitude            var location: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)            println(location)            CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: {(placemarks, error) -> Void in                println(manager.location)                if error != nil {                    println("Reverse geocoder failed with error" + error.localizedDescription)                    return                }                if placemarks.count > 0 {                    let pm = placemarks[0] as CLPlacemark                    println(pm.locality)                }                else {                    println("Problem with the data received from geocoder")                }

在我得到的日志中

//-122.0312186//37.33233141//C.CLLocationCoordinate2D//fatal error: unexpectedly found nil while unwrapping an Optional value

似乎该CLLocationCoordinate2DMake函数正在失败,然后导致该reverseGeocodeLocation函数中的致命错误。我是否在某处修改了格式?

答案1

小编典典

您永远不会对地理位置进行地理编码,而是传递manager.location。

看到: CLGeocoder().reverseGeocodeLocation(manager.location, ...

我认为这是一个复制粘贴错误,这就是问题所在-代码本身看起来不错-差不多;)

工作代码

    var longitude :CLLocationDegrees = -122.0312186    var latitude :CLLocationDegrees = 37.33233141    var location = CLLocation(latitude: latitude, longitude: longitude) //changed!!!    println(location)    CLGeocoder().reverseGeocodeLocation(location, completionHandler: {(placemarks, error) -> Void in        println(location)        guard error == nil else {            println("Reverse geocoder failed with error" + error.localizedDescription)            return        }        guard placemarks.count > 0 else {            println("Problem with the data received from geocoder")            return        }        let pm = placemarks[0] as! CLPlacemark        println(pm.locality)    })

4.7 Swift中swift中的switch 语句

4.7 Swift中swift中的switch 语句

/**

switch 语句

*/

let str = "aAbBacdef"

let str2 = "aAbBadef"

let str3 = "aAbBadeff"


// var array = [];

for c in ["A","a",str3]

{

switch c {

// case "a":

case "a","A":

print("ldd")


// 必须有

default:

print("dd")

}

}

/**

case "a":

case "A":

print("ldd")

C语言中, 这样写 无论遇到 a A 都会执行 print("ldd")

swift中就不允许这样子了,但是可以这样子写

case "a","A": 中间用逗号隔开

*/

// switch value {

// case pattern:

// code

// default:

// }

/**

c 语言中

case 下面有个 break

如果忘了写break 会顺序执行下面的语句,直到执行break

但是swift语言就是看到这一点就,不要break了。比较case里面的条件后,

执行完毕后面的语句就自动退出 switch语句了。

如果想要继续执行 fallthrough

*/

Android反向地理编码有问题

Android反向地理编码有问题

我用google 的Geocoding API 接口来处理反向地理编码  不知道为什么  显示不出  下面是代码 ,请告诉我原因

package com.test.location;


import java.util.List;


import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;


import android.support.v7.app.ActionBarActivity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {

private TextView positionTextView;

private LocationManager locationManager;

private String provider; //定义设备确定位置的变量

private static final int SHOW_LOCATION = 0;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
positionTextView = (TextView)
findViewById(R.id.position_text_view);
locationManager = (LocationManager) 
getSystemService(Context.LOCATION_SERVICE);
//获取所有可用的位置提供器
List<String> providerList = locationManager.getProviders(true);
if(providerList.contains(LocationManager.GPS_PROVIDER)){
provider = LocationManager.GPS_PROVIDER;
Toast.makeText(this," location provider is GPS",
Toast.LENGTH_SHORT).show();
}else if
(providerList.contains(LocationManager.NETWORK_PROVIDER)){
provider = LocationManager.NETWORK_PROVIDER; 
Toast.makeText(this," location provider is NETWORK",
Toast.LENGTH_SHORT).show();
}
else{
//当没有可用的位置提供器时,弹出Toast提示用户
Toast.makeText(this,"No location provider to use",
Toast.LENGTH_SHORT).show();
return;
}
//location对象包含了经纬度信息和海拔信息.
//将选择好的位置提供器传入getLastKnownLocation()方法中,就可以得到一个Location对象
Location location = locationManager.getLastKnownLocation(provider);
if(location !=null){
//显示设备现在的信息
showLocation(location);
showLocation_S(location);
}
//locationManager每隔5S检测一下位置的变化位置,但移动距离超过10M时,就会调用
//LocationListener的onLocationChanged()方法,并把新的位置信息作为参数传入
locationManager.requestLocationUpdates
(provider,5000,1,locationListener);
}
    
protected void onDestroy() {
super.onDestroy();
if(locationManager != null){
//关闭程序时将监听器移除,不然将会一直监听下去,赞同CPU资源
locationManager.removeUpdates(locationListener);
}
}

LocationListener locationListener = new LocationListener(){
@Override
public void onStatusChanged
(String provider, int status,Bundle extras){

}
@Override
public void onProviderEnabled(String provider){

}
@Override
public void onProviderDisabled(String provider){

}

@Override
public void onLocationChanged(Location location){
     //更新当前设备的位置信息
showLocation(location);
showLocation_S(location);
}
};

private void showLocation(Location location){
String currentPosition="latitude is " +location.getLatitude()
+"\n"+"longitude is "+location.getLongitude();
positionTextView.setText(currentPosition);
}

//对经纬度进行解析并翻译成中文地址
private void showLocation_S(final Location location){
new Thread(new Runnable(){
@Override
public void run() {
try{
//组装反向地理编码的接口地址
StringBuilder url = new StringBuilder();
url.append("http://maps.googleapis.com/maps/api/geocode/json?latlng=");
url.append(location.getLatitude()).append(",");
url.append(location.getLongitude());
url.append("&sensor=false");
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url.toString());
//在请求头信息头中指定语言,保证服务器会返回中文数据
httpGet.addHeader("Accept-Language","zh-CN");
HttpResponse httpResponse=httpclient.execute(httpGet);
if(httpResponse.getStatusLine().getStatusCode() == 200){
HttpEntity entity = httpResponse.getEntity();
String response = EntityUtils.toString(entity,"utf-8");

JSONObject jsonObject =new  JSONObject(response);
//获取results节点下的位置信息
JSONArray resultArray = jsonObject.getJSONArray("results");

if(resultArray.length() > 0){
JSONObject subObject = resultArray.getJSONObject(0);
//取出格式化后的位置信息
String address = subObject.getString("formatted_address");
Message message=new Message();
message.what = SHOW_LOCATION;
message.obj = address;
handler.sendMessage(message);
}
}

}catch(Exception e){
e.printStackTrace();
}
}
}).start();
}

private Handler handler = new Handler(){
public void handleMessage(Message msg){
switch(msg.what){
case SHOW_LOCATION:
String currentPosition = (String)msg.obj;
positionTextView.setText(currentPosition);
break;
default:
break;
}
}
};


}


angularjs – Ionic:反向地理编码

angularjs – Ionic:反向地理编码

我正在将GPS服务集成到我的应用程序中.我添加并注入了Cordova GPS插件.
我可以检索gps上ngCordova文档中显示的经度和纬度.这是我的方法:

.controller('geoLocationCtrl',function($scope,$window) {
    $window.navigator.geolocation.getCurrentPosition(function(position){
        $scope.$apply(function(){
            $scope.lat = position.coords.latitude;
            $scope.lng = position.coords.longitude;
        })

        console.log(position);
    })
})

我的挑战是如何使用上述内容返回用户的当前地址,状态和国家/地区.

解决方法

将经度和纬度转换为人类可读地址的过程称为反向地理编码.许多地图API支持Google Maps和Here Maps.以下是使用Google Maps API执行此操作的方法

添加Google Maps JavaScript API

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&language=en"></script>

查询API:这是一个角度控制器示例

app.controller('geoLocationCtrl',$window) {
  $window.navigator.geolocation.getCurrentPosition(function(position) {
    $scope.$apply(function() {
      $scope.lat = position.Coordinates.latitude;
      $scope.lng = position.Coordinates.longitude;

      var geocoder = new google.maps.Geocoder();
      var latlng = new google.maps.LatLng($scope.lat,$scope.lng);
      var request = {
        latLng: latlng
      };
      geocoder.geocode(request,function(data,status) {
        if (status == google.maps.GeocoderStatus.OK) {
          if (data[0] != null) {
            alert("address is: " + data[0].formatted_address);
          } else {
            alert("No address available");
          }
        }
      })
      console.log(position);
    })
  })
});

Angular上的传单Esri地理编码未导入地理编码类

Angular上的传单Esri地理编码未导入地理编码类

您应该导入:

import "leaflet/dist/leaflet.css";
import * as L from "leaflet";
import "esri-leaflet-geocoder/dist/esri-leaflet-geocoder.css";
import "esri-leaflet-geocoder/dist/esri-leaflet-geocoder";
import * as ELG from "esri-leaflet-geocoder";

然后像这样初始化插件:

  const searchControl = new ELG.Geosearch();
  const results = new L.LayerGroup().addTo(map);

    searchControl
      .on("results",function (data) {
        results.clearLayers();
        for (let i = data.results.length - 1; i >= 0; i--) {
          results.addLayer(L.marker(data.results[i].latlng));
        }
      })
      .addTo(map);

Demo

今天关于Swift中的反向地理编码位置的介绍到此结束,谢谢您的阅读,有关4.7 Swift中swift中的switch 语句、Android反向地理编码有问题、angularjs – Ionic:反向地理编码、Angular上的传单Esri地理编码未导入地理编码类等更多相关知识的信息可以在本站进行查询。

本文标签: