如何在Google地图中设置几个特定位置?
How to set several specific locations in Google Map?
我想为德黑兰的自行车站申请 android。为此,我需要在地图上显示特定的静态位置,如下所示:
我尝试了 Github 中的几个库,但都没有用。我还添加了一个额外的位置 (double latitude1 = 35.747394; double longitude1 = 51.267577;)
但地图上没有显示位置标记。
如果有人能帮助我,我将不胜感激。
这是我的代码:
MainActivity.java
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.MapFragment;
import com.playpersia.bicyclemap.R;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsActivity extends FragmentActivity {
double latitude1 = 35.747394;
double longitude1 = 51.267577;
// Google Map
private GoogleMap googleMap;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
Uri gmmIntentUri = Uri.parse("geo:35.694677, 51.394871");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); //***Change Here***
mapIntent.setPackage("com.google.android.apps.maps");
if (mapIntent.resolveActivity(getPackageManager()) != null) {
startActivity(mapIntent);
finish();
}
try {
initilizeMap();
MarkerOptions markerOptions1 = new MarkerOptions().position(
new LatLng(latitude1, longitude1)).title("بوستان جوانمردان");
googleMap.addMarker(markerOptions1);
} catch (Exception e) {
e.printStackTrace();
}
}
@
Override
protected void onResume() {
super.onResume();
initilizeMap();
}
private void initilizeMap() {
if (googleMap == null) {
SupportMapFragment mapFragment = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(
R.id.map));
if (googleMap != null) {
Toast.makeText(getApplicationContext(), "خطا", Toast.LENGTH_LONG).show();
}
}
}
}
我希望你正在尝试在你的应用程序中显示地图 itself.You 如果你阅读你的日志猫就会发现错误。 googleMap 变量未初始化。因此,如果添加标记,它将抛出空指针异常。您应该使用 getMapAsync 回调来初始化 googleMap 变量,然后您可以添加 markers.This 如果您的地图显示并且添加标记不起作用,则将起作用。如果地图没有显示 api 键的问题。更正并删除意图代码并尝试此操作。
查看 Google Places API 文档。它允许您查询各种类别的地点信息,例如:机构、著名景点、地理位置等。您可以按邻近度或文本字符串搜索地点。
使用Nearby Search which lets you search for places within a specified area. You can refine your search request by supplying keywords or specifying the type of place you are searching for. Here is the list of supported types.
Sample HTTP URL of the following form:
https://maps.googleapis.com/maps/api/place/nearbysearch/output?parameters
where output
may be either of the following values:
json
(recommended) indicates output in JavaScript Object Notation (JSON)
xml
indicates output as XML
以下示例是对澳大利亚悉尼某点 500 米半径内 'transit_station' 类型地点的搜索请求:
https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&radius=500&type=transit_station&key=YOUR_API_KEY
检查这些相关链接和 tutorial:
- Finding nearest locations using Google Maps API
希望对您有所帮助!
我想为德黑兰的自行车站申请 android。为此,我需要在地图上显示特定的静态位置,如下所示:
我尝试了 Github 中的几个库,但都没有用。我还添加了一个额外的位置 (double latitude1 = 35.747394; double longitude1 = 51.267577;)
但地图上没有显示位置标记。
如果有人能帮助我,我将不胜感激。
这是我的代码:
MainActivity.java
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.MapFragment;
import com.playpersia.bicyclemap.R;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsActivity extends FragmentActivity {
double latitude1 = 35.747394;
double longitude1 = 51.267577;
// Google Map
private GoogleMap googleMap;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
Uri gmmIntentUri = Uri.parse("geo:35.694677, 51.394871");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); //***Change Here***
mapIntent.setPackage("com.google.android.apps.maps");
if (mapIntent.resolveActivity(getPackageManager()) != null) {
startActivity(mapIntent);
finish();
}
try {
initilizeMap();
MarkerOptions markerOptions1 = new MarkerOptions().position(
new LatLng(latitude1, longitude1)).title("بوستان جوانمردان");
googleMap.addMarker(markerOptions1);
} catch (Exception e) {
e.printStackTrace();
}
}
@
Override
protected void onResume() {
super.onResume();
initilizeMap();
}
private void initilizeMap() {
if (googleMap == null) {
SupportMapFragment mapFragment = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(
R.id.map));
if (googleMap != null) {
Toast.makeText(getApplicationContext(), "خطا", Toast.LENGTH_LONG).show();
}
}
}
}
我希望你正在尝试在你的应用程序中显示地图 itself.You 如果你阅读你的日志猫就会发现错误。 googleMap 变量未初始化。因此,如果添加标记,它将抛出空指针异常。您应该使用 getMapAsync 回调来初始化 googleMap 变量,然后您可以添加 markers.This 如果您的地图显示并且添加标记不起作用,则将起作用。如果地图没有显示 api 键的问题。更正并删除意图代码并尝试此操作。
查看 Google Places API 文档。它允许您查询各种类别的地点信息,例如:机构、著名景点、地理位置等。您可以按邻近度或文本字符串搜索地点。
使用Nearby Search which lets you search for places within a specified area. You can refine your search request by supplying keywords or specifying the type of place you are searching for. Here is the list of supported types.
Sample HTTP URL of the following form:
https://maps.googleapis.com/maps/api/place/nearbysearch/output?parameters
where
output
may be either of the following values:
json
(recommended) indicates output in JavaScript Object Notation (JSON)xml
indicates output as XML
以下示例是对澳大利亚悉尼某点 500 米半径内 'transit_station' 类型地点的搜索请求:
https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&radius=500&type=transit_station&key=YOUR_API_KEY
检查这些相关链接和 tutorial:
- Finding nearest locations using Google Maps API
希望对您有所帮助!